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;

}


c program to parsing a string with delimiter in-between [using strtok]

C program to parsing a string with delimiter in-between [using strtok]


#include <stdio.h>

#include <string.h>

int main (void)
{
  char str[] = "neelkanth<delimiter>google<delimiter>stallone";
  char *temp = str;
  char *delim = "<delimiter>";
  char *token;

  do {
    printf("Before Tokenizing : %s\n", temp);
    token = strstr(temp , delim);

    if (token)
      *token = '\0';

    printf("After Tokenizing :%s\n", temp);
    temp = token+strlen(delim);


  }while(token!=NULL);


  return 0;
}


Neelkanth_98$ ./a.out
Before Tokenizing : neelkanth<delimiter>google<delimiter>stallone
After Tokenizing :neelkanth
Before Tokenizing : google<delimiter>stallone
After Tokenizing :google
Before Tokenizing : stallone
After Tokenizing :stallone


C program to write environment variables and their values into file and read the variables and their values from the file (using strtok)

C program to write environment variables and their values into file and read the variables and their values from the file (using strtok)


Neelkanth_39$ cat neel.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void func_read();
void func_write(char **envp);

void func_write(char **envp)
{

    FILE *fp = NULL;
    char a[1000];
    int i = 0;
    int len = 0;

    printf("\n WRITING ENVIRONMENT VARIABLES INTO FILE \n");
    fp = fopen ("file.txt", "w");
    if (fp == NULL){
        printf("failed to open %s", "file.txt");
        return ;
    }


    while(envp[i] != NULL) {
          strcat(a , envp[i]);
          strcat(a , "@");
          i++;
    }
    printf("%s\n",a);
    len = strlen(a);

    a[len+1] = '\0';

    fwrite( a, strlen(a), 1, fp);
    fclose(fp);

    func_read();

}

void func_read()
{
    char **envp;
    char a[1000];
    FILE *fp;
    char *pt;
    char temp[10][100];
    int i = 0;

    printf("\n READING ENVIRONMENT VARIABLES AND THEIR VALUES FROM FILE \n");
    fp = fopen ("file.txt", "r");
    if (fp == NULL) {
        printf("failed to open %s", "file.txt");
        return ;
    }

    fscanf(fp, "%s", a);

    pt = strtok (a,"@");
    while (pt != NULL) {
        printf("%s\n", pt);
        strcpy(temp[i], pt);
        pt = strtok (NULL, "@");
        i++;
    }
    printf("temp[0]: %s\n", temp[0]);
    printf("temp[1]: %s\n", temp[1]);
    printf("temp[2]: %s\n", temp[2]);

    char *name, *value;

    name = strtok(temp[0], "=");
    printf("name: %s\n", name);
    value = strtok(NULL, "=");
    printf("value: %s\n", value);

    return;

}

int main()
{

    char *envp[] = {"stallone=rambo" ,"google=god", "hrithik=me", NULL };

    printf("\n PASS ENVIRONMENT VARIABLES TO WRITE TO FILE FUNCTION \n");
    func_write(&envp[0]);

    return 0;
}




Output:


Neelkanth_39$ gcc neel.c
Neelkanth_39$ ./a.out

 PASS ENVIRONMENT VARIABLES TO WRITE TO FILE FUNCTION

 WRITING ENVIRONMENT VARIABLES INTO FILE
stallone=rambo@google=god@hrithik=me@

 READING ENVIRONMENT VARIABLES AND THEIR VALUES FROM FILE
stallone=rambo
google=god
hrithik=me
temp[0]: stallone=rambo
temp[1]: google=god
temp[2]: hrithik=me
name: stallone


c program to copy values from double pointer array into file

C program to copy values from double pointer array into file


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


void func(char **envp)
{
    FILE *fp = NULL;
    fp = fopen ("dhcp", "w");
    if (fp == NULL){
        printf("failed to open %s", "dhcp");
        return ;
    }

    int i = 0;
    while(envp[i] != NULL) {
        fwrite( envp[i], strlen(envp[i])+1, 1, fp);

        i++;
    }
    fclose(fp);
}


int main()
{

    char *envp[] = {"stallone\0" ,"google\0", NULL };

    func(&envp[0]);

    return 0;
}