Posix Thread example

Get a file name from the command line to the main function and create that file. When SIGPWR is received, each thread should write the following line to the created file.

The sum from ``low'' to ``i'' is ``myresult''.

Also in the main function, before the threads are created, print the process id of the parent process so that you can send SIGPWR to it using the ``kill'' function from another terminal.


Implementation in C

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

#define ARRAYSIZE 1000
#define THREADS 10
#define TRUE 1
#define FALSE 0

void *slave(void *myid);
void pwrhandler(int signal);//Signal handling function for PWR signal

/* shared data */
int data[ARRAYSIZE]; /* Array of numbers to sum */
int sum = 0;
pthread_mutex_t mutex; /* mutually exclusive lock variable */
int wsize;          /* size of work for each thread */
/* end of shared data */

FILE *fp;   //File pointer to use create a file
int flag=FALSE;   //Initially flag get FALSE => 0

void *slave(void *myid)
{
    int i,low,high,myresult=0;
    low = (int) myid * wsize;
    high = low + wsize;
  
    for (i=low;i<high;i++){
        sleep(2);
        myresult += data[i];
        if(flag==TRUE)
            break;
    }

    printf("The sum from low=%d to i=%d is myresult=%d \n",low,i,myresult);
    fprintf(fp,"The sum from low=%d to i=%d is myresult=%d \n",low,i,myresult); //Write to the FILE
  
    //printf("I am thread:%d low=%d high=%d myresult=%d \n",(int)myid,low,high,myresult);
  
    pthread_mutex_lock(&mutex);
    sum += myresult;  /* add partial sum to local sum */
    pthread_mutex_unlock(&mutex);

    return;
}

//Signal handler for PWR signal
void pwrhandler(int signum){
    signal(SIGPWR, pwrhandler);    //Reset the handler
    flag = TRUE;   //Set the flag to TRUE => 1
}

main(int argc, char *argv[])
{
    int i;
    pthread_t tid[THREADS];

    /* set the PWR signal handler to catch PWR signal */
    signal(SIGPWR, pwrhandler);
  
    if(argv[1] == NULL){
        printf("WARNING : Executable file will run again with file name..........\n");
        exit(0);
    }
    if(argc==2){
        /*Get the process ID*/
        printf("Parent process ID : %d\n", getpid());
        fp=fopen(argv[1],"w"); //Open the file
        printf("File %s successfully created......\n", argv[1]);
    }
    if(argc != 2){
        printf("WARNING : Cannot open a file............\n");
        exit(0);
    }

    pthread_mutex_init(&mutex,NULL); /* initialize mutex */
 
    wsize = ARRAYSIZE/THREADS;       /* wsize must be an integer */

    for (i=0;i<ARRAYSIZE;i++)        /* initialize data[] */
        data[i] = i+1;

    for (i=0;i<THREADS;i++) /* create threads */
        if (pthread_create(&tid[i],NULL,slave,(void *)i) != 0)
            perror("Pthread_create fails");

    for (i=0;i<THREADS;i++) /* join threads */
        if (pthread_join(tid[i],NULL) != 0)
            perror("Pthread_join fails");

    printf("The sum from 1 to %i is %d\n",ARRAYSIZE,sum);
    fclose(fp); //Close the file
    printf("Data has been written to file successfully.......\n");
}

Comments

Popular posts from this blog

Solve the Maze by DFS (Depth First Search)

Rabin Karp Algorithm

Text-to-Speech converter