C Programming Tutorial

 
 
 
 
#include <stdio.h>
#include \"headfork.h\"
#include \"headsem.h\"
#include \"headshr.h\"

main()
{
    int i,j,no,nprc,t,id,shmid1,shmid2,shmid3;
    int *arr,*index,*lock;

    lock=(int *)shared(sizeof(int)*2,&shmid1);
    arr=(int *)shared(sizeof(int)*10,&shmid2);
    index=(int *)shared(sizeof(int)*2,&shmid3);

    printf(\"Enter the limit of an array: \");
    scanf(\"%d\",&no);

    printf(\"\\nEnter the values :\");
    for(i=0;i<no;i++)
    {
        printf(\"\\narr[%d] : \",i);
        scanf(\"%d\",(arr+i));
    }

    printf(\"\\nEnter the constant value : \");
    scanf(\"%d\",&t);

    lock_init(lock);

    *index=0;

    printf(\"\\nEnter the no.of proc : \");
    scanf(\"%d\",&nprc);

    id=p_fork(nprc);
    j=0;
    for(i=0;i<no;i++)
    {
        locksem(lock);
        j=*index;
        *index=*index + 1;
        unlock(lock);

        if(j>no)
            break;
        *(arr+j)+=t;
    }

    p_join(nprc,id);

    printf(\"\\nAfter adding %d const to array is :\",t);

    for(i=0;i<no;i++)
        printf(\"\\n%d\",*(arr+i));
}

OUTPUT

knoppix@ttyp0[pp]$ cc add_const_ss.c
knoppix@ttyp0[pp]$ ./a.out

Enter the limit of an array: 5

Enter the values :
arr[0] : 2
arr[1] : 4
arr[2] : 1
arr[3] : 3
arr[4] : 6

Enter the constant value : 3

Enter the no.of proc : 3

After adding 3 const to array is :
5
7
4
6
9

 
 
Didn't find what you were looking for? Find more on PROGRAM FOR ADD A CONSTANT TO EACH ARRAY ELEMENETS USING SELF SCHEDULING