Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <sys/wait.h>
- int c_i;
- int create_and_pause(int i)
- {
- int pid = fork();
- if (pid<0)
- {
- printf("error");
- }
- if(pid==0)
- {
- printf("[Child process %d: %d]Was created by %d and will pause\n",i,getpid(),getppid());
- //pause();
- c_i=i;
- raise(SIGSTOP);
- }
- return pid;
- }
- int main(int argc, char * argv[])
- {
- int i;
- int N =5;
- int d[5] = {1,1,2,1,2}; //na antikatastathei me loop pou pairnei argv
- int p[N];
- int father_p = getpid();
- for(i = 0; i<N; i++)
- {
- if(getpid()==father_p)
- {
- p[i] = create_and_pause(i);
- sleep(1);
- }
- }
- if(getpid() == father_p)
- {
- printf("tis ftiaksame oles yay \n");
- for(i =0; i<N; i++)
- {
- kill(p[i],SIGCONT);
- }
- printf("ekana se oles continue\n");
- }
- int j = 0 , cnt = 0 ;
- if(getpid()!=father_p)
- {
- sleep(2);
- printf("[Child Process %d: %d]Is starting \n",c_i,getpid());
- for(j =0; j<10;j++) //tha antikatastathei apo while true
- {
- cnt++;
- sleep(d[c_i]);
- printf("process %d counter is %d\n",getpid(),cnt);
- }
- printf("eimai h %d kai molis teliosa\n",getpid());
- exit(0);
- }
- if(getpid() == father_p)
- {
- printf("twra kanw wait poutanes\n");
- for(i=0;i<N;i++)
- {
- wait(NULL);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement