c - How to make a process continue it's execution when it's waiting a signal with pause() and it doesn't recieve it -
i'm new forum , i've been googling , searching here problem didn't find it. anyway i'm not expert in searching in forum please excuse me if has been answered. ok let's point: i'm doing small project college have following code written in c:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> int arb,a,b,x,y,z,i; void senyal(){ //signal usr1 method if(getpid()==a){ printf("i'm process, i've received signal\n"); system("pstree"); } else if(getpid()==b){ printf("i'm b process, i've received signal\n"); system("pstree"); } else if(getpid()==x){ printf("i'm x process, i've received signal\n"); execlp("ls", "ls", "-al", "*.c", null); } else if(getpid()==y){ printf("i'm y process, i've received signal\n"); execlp("ls", "ls", "-al", "*.c", null); } } int main (int argc, char *argv[]){ char p=argv[1][0]; int num=(int)p; pid_t pid; pid_t pid2; pid_t pid3; arb=getpid(); pid=fork(); //creates father arb , child if(pid==-1){ printf ("error"); } else if(pid==0){ // child printf ("i'm process: pid %d. father %d \n",getpid(), arb); a=getpid(); if(num==65||num==97){ num=a;} pid2=fork(); //a father , creates b child if(pid2==-1){ printf ("error2");} else if(pid2==0){// b child b=getpid(); if(num==66||num==98){ num=b;} printf("i'm b process , pid %d. father %d. grandfather %d \n",getpid(),getppid(),arb); pid3=fork();//creates x switch(pid3){ case -1: printf("error3"); break; case 0: printf("i'm x process, pid %d. father %d. grandfather %d. great grandfather %d\n",getpid(),getppid(),a,arb); x=getpid(); if(num==88||num==120){ num=x;} signal(10,senyal); printf("i'm x (%d) , die\n",x); exit(x); break; default: break; } pid3=fork();//creates y switch(pid3){ case -1: printf("error no podido crear el proceso hijo\n"); break; case 0: printf(i'm y process, pid %d. father %d. grandfather %d. great grandfather %d\n",getpid(),getppid(),a,arb); y=getpid(); if(num==89||num==121){ num=y;} signal(10,senyal); printf("i'm y (%d) , die\n",y); exit(y); break; default: break; } pid3=fork();//creates z switch(pid3){ case -1: printf("couldn't create children proccess\n"); break; case 0: printf("i'm z process, pid %d. father %d. grandfather %d. great grandfather %d\n",getpid(),getppid(),a,arb); z=getpid(); printf("i'm z (%d) , die\n",z); sleep(argc); // kill(num,sigusr1);//sends signal of other process (a/b/x/y) exit(z); break; default: break; } wait();//wait childrens' death wait(); wait(); printf("i'm b(%d) , die\n",b); exit(b);//b's death } else{//father signal(10,senyal); wait();//await b's death printf("i'm (%d) , die\n",a); exit(a); //a's death } } else{//padre arb printf ("\ni'm process arb: pid %d\n",getpid()); signal(10,senyal); wait(); //await a's death } wait(); printf("i'm arb (%d) , die\n",arb); exit (0);//arb's death }
so code supposed create proccess tree , receive parametrer indicates pid of proccess needs recieve signal of type usgr1 process z sending. problem here want every process prepared receive signal, in case process doesn't recieve (for example process waiting , ready receive signal z z sends b) don't know how tell process go on , not stuck in pause forever. that's it, sorry if commited grammar or explanation errors i'm spain way. willing answer!
from man page of pause():
description
pause() causes calling process (or thread) sleep until signal delivered either terminates process or causes invocation of signal-catching function.
so need thread delays sleep() or similar , sends signal anyway after while. or use else pause, sleep.
Comments
Post a Comment