C program to change to default action inside the signal handler

C program to change to default action inside the signal handler


#include<stdio.h>

#include<signal.h>

#include<unistd.h>



void sig_handler(int signo)

{

    if (signo == SIGSEGV) {

        printf("received SIGSEGV\n");

        signal(signo, SIG_DFL);



/*  The raise() function sends a signal to the calling process or thread.

  In a single-threaded program it is equivalent to

   kill(getpid(), sig);

 */

        raise(signo);

    }

}



int main(void)

{    /* register signal handler */

    if (signal(SIGSEGV, sig_handler) == SIG_ERR) {

        printf("\ncan't catch SIGSEGV\n");

    }



    while(1)

        sleep(1);

    return 0;

}


C program to send signal (SIGTERM) from the signal handler(SIGINT) of one process to another process which is registered with SIGTERM

C program to send signal (SIGTERM) from the signal handler(SIGINT) of one process to another process which is registered with SIGTERM


Neelkanth_98$ cat signal1.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
    int process_id;
    char process_id_from_file[10];
    FILE *pid_fd;
    char command[100];

    sprintf(command,"pidof %s", "test_process");
    pid_fd = popen(command, "r");
    if (pid_fd == NULL)
    {
        return;
    }

    if (fgets(process_id_from_file, sizeof process_id_from_file , pid_fd) == NULL)
    {
        return;
    }
    pclose(pid_fd);

    process_id = atoi(process_id_from_file);

    if (signo == SIGINT) {
        printf("received SIGINT from another process\n");
        kill(process_id, SIGTERM);
        printf("send SIGTERM to another process\n");
        return;
    }
}

int main(void)
{    /* register signal handler */
    if (signal(SIGINT, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGINT\n");

    while(1)
        sleep(1);
    return 0;
}
Neelkanth_98$ cat signal2.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
    if (signo == SIGTERM)
        printf("received SIGTERM from test process\n");
}

int main(void)
{    /* register signal handler */
    if (signal(SIGTERM, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGTERM\n");

    while(1)
        sleep(1);
    return 0;
}


C program to print userid and group id

C  program to print userid and group id 


#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include <grp.h>


int main()
{
    struct passwd *pwd;
    struct group  *grp;
    int uid, gid;


    pwd = getpwnam("nobody");
    if (pwd == NULL) {
        printf("Failed to get uid");
    }
    uid = pwd->pw_uid;

    grp = getgrnam("root");
    if (grp == NULL) {
        printf("Failed to get gid");
    }
    gid = grp->gr_gid;


    printf("\n %d  %d \n", uid, gid);


    return 0;
}
~


C program to implement error handling

C program to implement error handling


#include <stdio.h>
#include <errno.h>

extern int errno ;

int main ()
{
  FILE * pFile;
  pFile = fopen ("unexist.ent","rb");
  if (pFile == NULL)
  {
    perror ("The following error occurred");
    printf( "Value of errno: %d\n", errno );
  }
  else
    fclose (pFile);
  return 0;
}
If file unexist.ent does not exit then it will produce following result:
The following error occurred: No such file or directory
Value of errno: 29


C program to implement software timer

C program to implement software timer


#include <stdio.h>
#include <time.h>
int main()
{
    time_t start = time(NULL);
   for (;;)
    {
        if (time(NULL) > start + 10) {
         printf("10s timeout!\n");               
         break;
    }
}
    return 0;

}