Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to create header file for fork,join,shared memory,lock,free shared memory and free semaphore

Posted By: Bogart Fischer     Category: C Programming     Views: 4314

Program to create header file for fork,join,shared memory,lock,free shared memory and free semaphore.

Code for Program to create header file for fork,join,shared memory,lock,free shared memory and free semaphore in C Programming

# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/shm.h>
# include <sys/sem.h>

int processfork(int x)
{
    int j;
    for(j=1;j<x;j++)
    {
        if(fork()==0)
            return j;
    }
    return 0;
}

void processjoin(int x,int id)
{
    int i;
    if(id==0)
    {
        for(i=1;i<x;i++)
        {
            wait(0);
            
        }
    }
    else
    {
        exit(0);
    }
}

void lockinit(int *lock1)
{
    union semun
    {
        int val;
        struct semid_ds *x;
        ushort *y;    
    }arg;
    int j;

    j=semget(IPC_PRIVATE,1,0666|IPC_CREAT);
    
    *lock1=j;

    arg.val=1;

    semctl(*lock1,0,SETVAL,arg);

//    printf("After lockinit\n");
    
}

void locksem(int *lock1)
{
    struct sembuf oper;
    
    oper.sem_num=0;
    oper.sem_op=-1;
    oper.sem_flg=0;

//    printf("before Locking\n");
    semop(*lock1,&oper,1);
}

void unlock(int *lock1)
{
    struct sembuf oper;
    
    oper.sem_num=0;
    oper.sem_op=1;
    oper.sem_flg=0;

    semop(*lock1,&oper,1);
}

void freesharedmem(int shmid)
{
    struct shmid_ds *buf;
    shmctl(shmid,IPC_RMID,buf);
}

void freesemaphore(int semid)
{
    semctl(semid,0,IPC_RMID,0);
}

  
Share: 



Bogart Fischer
Bogart Fischer author of Program to create header file for fork,join,shared memory,lock,free shared memory and free semaphore is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!