Basic Unix Programming

Basic Unix Programming

Signal Handler

—————–

/* Sample program to handle signals */

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

void * myhandler(int myint)
{
   printf(“nHey, I got signalled!!nn”);
   exit(0);
}

int main()
{
   signal( SIGINT, (void *)  myhandler );

   while(1) {
    printf(“I am in an infinite loop!n”);
    sleep(1);
   }
}

 


 

/* Skeletal Shell for simple command */

main (argc, argv, envp)
int argc;
char **argv, **envp;
{
   int child, status; char command [80], *comargv[10];

   do {
       printf (“n$ “); /* Print prompt string */
       getline (command);
       parse (command, comargv);
       if ((child = fork()) != 0)
          /* Parent Shell executes here. */
          while (child != wait (&status))
          ;
       else /* Child executes here. */
      execve (command, comargv, envp);
    } while (1);
}


/* Example of use of fork system call */
#include <stdio.h>

main()
{
  int pid;
 
  pid = fork();

  if (pid < 0) { /* error occurred */
      fprintf(stderr, “Fork failed!n”);
      exit(-1);
  }

  else if (pid==0) { /* child process */
      printf(“I am the child, return from fork=%dn”, pid);
      execlp(“/bin/ps”, “ps”, NULL);
  }
  else { /* parent process */
      printf(“I am the parent, return from fork, child pid=%dn”, pid);
      printf(“Parent exiting!n”);
      exit(0);
  }
}

Comments