Skip to main content

Folk and signals - C





I maked sample process in c it plays with folk function to make it pause and resume while clicking ctrl+c
If you have any questions please comment.

Code:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void reverse_handler(int sig);
_Bool isPause=0;
_Bool isRunning=1;

int main()
{
int ppid;
int counter=0;
//make parent respond for ctrl+c (pause,resume).
signal(SIGINT,reverse_handler);
while(isRunning){
while(isPause==0)
{
/*code exec while process is resuming */
printf("\nc:%d",counter++);
fflush(stdout);
sleep(1);
}
//close parent after child is alive.
if((ppid=fork())==0){ exit(0); }
//make child respond for ctrl+c (pause,resume).
signal(SIGINT,reverse_handler);
//keep child alive and listening.
while(isPause==1){ /*code exec while process is pausing */ sleep(1); }
}
return 0;
}
//if process is pause made it resume and vice versa.
void reverse_handler(int sig){
if(isPause==0){
printf("\nPaused");
fflush(stdout);
isPause=1;
}
else if(isPause==1){
printf("\nresuming");
fflush(stdout);
isPause=0;
}
}

Thanks..

Popular posts from this blog

Collision detection Challenge (Vanilla Js)

Collision detection If You Want Check My Code  GitHub ;  Link please feedback me if code need any modifications or new ideas Thanks ..

eQuran V1

E-Quran link:  E-Quran i created a website for Quran Kareem with pure javascript and CSS and HTML without any library and that's the first version wait for me with the next project but don't forget to contact me for any question or suggestions  thank you......