I have a problem I'm trying to run a program that have a user defined number of consumer and clients. The pipe works fine but the problem is that between characters I have a filename of the file that I'm writing to. It generates a good number of chars client get it right too. Between chars in client there is a client file name and between chars in consument there is consument file name.
File contains "S/prod/prod-92721W/prod/prod-92721X".
The second problem is that all producents have the same char generated and I wanted to have a different set of char for every producent.
Can somebody explain what I'm doing wrong
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <sys/param.h>
void tworz_konsumentow (int *potok, long ilekonsum);
void tworz_producentow (int *potok, long ileproducentow, long ileznakow);
void wygeneruj_plik (int ile_znakow);
void init(int argc, char **argv, long *ileproducentow, long *ilekonsum, long *ileznakow)
{
if(argc!=4)
{
printf("Z?a ilo?? argumentów");
exit(1);""
}
char *koniec;
*ileproducentow = strtol(argv[1], &koniec, 10); //konwertuje wszystko na posta? liczbow? typu long
*ilekonsum = strtol(argv[2], &koniec, 10);
*ileznakow = strtol(argv[3], &koniec , 10);
if(*ileznakow <=0 || *ileproducentow <=0 || *ilekonsum <=0)
{
printf("Podano z?? warto?c argumentu");
exit(1);
}
}
int main(int argc, char **argv)
{
long ileproducentow , ilekonsum, ileznakow;
init(argc, argv, &ileproducentow, &ilekonsum, &ileznakow);
int potok[2];
if (pipe(potok) == -1)
{
perror("Error is: ");
exit(1);
}
tworz_producentow(potok, ileproducentow, ileznakow);
tworz_konsumentow(potok, ilekonsum);
close(potok[0]);
close(potok[1]);
}
void tworz_producentow (int *potok, long ileproducentow, long ileznakow)
{
srand(time(NULL));
FILE *file ;
int zapis ;
char znak[1] ;
for (int i; i<ileproducentow; i++)
{
switch(fork())
{
case -1:
perror("Fork error:");
exit(1);
case 0:
close(potok[0]); //blokuje czytanie drugiemu procesowi
char filename[sizeof("./prod/prod-95844.txt")] ;
sprintf(filename,"./prod/prod-%d.txt",getpid());
file = fopen(filename, "a");
for (int j=0; j<ileznakow; j++)
{
znak[0] = 'A' + (random() % 26);
fputs(znak, file);
zapis = write(potok[1], znak, 1); //write wpisuje znak do potoku
if (zapis == -1)
{
perror("error is:");
fclose(file);
exit(1);
}
}
close(potok[1]);
fclose(file);
exit(0);
}
}
}
void tworz_konsumentow(int *potok, long ilekonsum)
{
int desc;
char znak[1];
FILE *file;
for (int i = 0 ; i < ilekonsum; i++)
{
switch(fork())
{
case -1:
perror("Fork error:");
exit(1);
case 0:
close(potok[1]);
char filename[sizeof("./cons/cons-95844.txt")] ;
sprintf(filename,"./cons/cons-%d.txt",getpid());
file = fopen(filename, "a");
while(1)
{
desc = read(potok[0], znak, 1); //
if (desc == 0)
{
close(potok[0]);
fclose(file);
exit(1);
}
else if (desc == -1)
{
perror ("Blad odczytu danych z potoku");
fclose(file);
exit(1);
}
else
{
fputs(znak, file);
}
}
}
}
}
question from:
https://stackoverflow.com/questions/65878735/problem-when-writing-characters-into-file-in-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…