So, I'm experiencing two small issues but I just can't seem to find a solution for it so I am here to ask for some assistance from you guys!
So, I am teaching myself how to deal with signalling and forking and my program works great except two small minor issues:
- the output text file isn't what I want it to be (I'll show how it's suppose to be vs what I'm getting)
- and the program doesn't end at the end — it just hangs...
First off, here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h> // for wait
#include <sys/wait.h> // for wait
#include <fcntl.h>
void action(int);
void action(int dummy){
sleep(1);
printf("Switching\n");
}
int main(int argc, char *argv[])
{
pid_t pid, pid1, pid2;
int m = atoi(argv[1]), i = 0, x = 0, s = 0, timeinms=1000000, fd, n, xx = 0;
const char* filename = "input.txt";
const char* filename2 = "output.txt";
char buffer[40];
if((pid=fork())>0)//parent
{
sleep(2 * m);
for (s = 0; s < 5; s++)
{
if((pid2=fork())>0)
{
/*open file, read x from file, close file*/
fd = open(filename, O_RDONLY);
read(fd, buffer, sizeof(buffer));
close(fd);
/*increment x*/
xx = atoi(buffer);
xx = xx + 1;
/*open file, write new x to file, close file*/
n = sprintf(buffer,"%d\n", xx);
fd = open(filename, O_WRONLY | O_CREAT, 0644);
write(fd,buffer,n);
close(fd);
/*open another file, write the output to it, close it*/
n = sprintf(buffer, "%d", xx);
fd = open(filename2, O_WRONLY | O_CREAT, 0644);
write(fd, "Parent:\n", 8);
write(fd, "\n", 1);
write(fd, buffer,n);
//write(fd, "\n\n", 2);
close(fd);
usleep(timeinms); //sleep(timeins);
printf("%s\n","Parent run!");
signal(SIGUSR1, action);
}
else if ((pid2=fork())==0)
{
/*open file, read x from file, close file*/
fd = open(filename, O_RDONLY);
read(fd, buffer, sizeof(buffer));
close(fd);
/*increment x*/
xx = atoi(buffer);
xx = xx * 10;
/*open file, write new x to file, close file*/
n = sprintf(buffer,"%d", xx);
fd = open(filename, O_WRONLY | O_CREAT, 0644);
write(fd,buffer,n);
close(fd);
/*open another file, write the output to it, close it*/
n = sprintf(buffer, "%d", xx);
fd = open(filename2, O_WRONLY | O_CREAT, 0644);
write(fd, "Child:\n", 7);
write(fd,"\n",1);
write(fd, buffer,n);
close(fd);
usleep(timeinms); //sleep(timeins); //nanosleep(&mytimeinns, &mytimeinns1);
printf("%s\n","Child run!");
pause();
}
else
{
//printf("%s\n","Error: fork() failed.");
return 1;
}
}
}
else
{
while(i < m)//child
{
printf("hello %d\n", x);
x++;
i++;
sleep(1);
}
i = 0; // Reset i back to 0 for the while loop
x = 0; // Reset x back to 0 so it can recount from 0 to m
if((pid1=fork())>0)//child
{
kill(getpid(), SIGUSR1);
}
else
while(i < m)//grandchild
{
printf("hi %d\n", x);
x++;
sleep(1);
i++;
}
}
return 0;
}
Output should look like:
hello 0
hello 1
hello 2
hello 3
hello 4
hi 0
hi 1
hi 2
hi 3
hi 4
Parent :
x value after operation : 1
=======================
Child :
x value after operation : 10
=======================
Parent :
x value after operation : 11
=======================
Child :
x value after operation : 110
=======================
Parent :
x value after operation : 111
=======================
Child :
x value after operation : 1110
=======================
Parent :
x value after operation : 1111
=======================
Child :
x value after operation : 11110
=======================
Parent :
x value after operation : 11111
=======================
Child :
x value after operation : 111110
=======================
So far however,
The first part with the hello and hi is working perfectly, but then (I have the second part writing to a file), the output file is showing me the following
Child:
111110
So I know the increment is working fine but it's just not writing to file properly (increment, write to file, newline, write to file again.. etc).
Oh and the console output is as follows:
So as you guys can see it's not ending the program.. Just hanging there.
Any help would be much appreciated!
Aucun commentaire:
Enregistrer un commentaire