C Programming Tutorial

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

main()
{
    int id,i,n,npr,sid1,sid2;
    int *a1,*a2;

    int p_fork(int);
    void p_join(int,int);

    sid1=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);

    a1=(int *)shmat(sid1,0,0);

    sid2=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);

    a2=(int *)shmat(sid2,0,0);

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

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

    printf(\"Enter the array elements :\");
    for(i=0;i<n;i++)
        scanf(\"%d\",&a2[i]);

    for(i=0;i<n;i++)
        *(a1+i)=0;

    id=p_fork(npr);

    for(i=id;i<n;i+=2)
        *(a1+i)=*(a2+i);

    p_join(npr,id);

    printf(\"\\nNew copied array is : \");

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

int p_fork(int x,int id)
{
    int t;
    for(t=1;t<x;t++)
    {
        if(fork()==0)
            return t;
    }
    return 0;
}

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

OUTPUT

knoppix@ttyp0[pp]$ cc cparr_nproc.c
knoppix@ttyp0[pp]$ ./a.out
Enter the no. of proc :2
Enter the limit of array :5
Enter the array elements :
2
4
5
1
3
New copied array is :
2
4
5
1
3

    Related Post:
  1. Program to compare one string s2 to another string s1. Print the difference of first unmatched characters (Use strcmp function from string.h)

  2. Example of using preprocessor - 4 in different files

  3. Program of passing character array to function

  4. Program to get string after user specified position

  5. Program to display dimond triangle

  6. Program that accepts characters in lower case, stores them in an array and prints them in the upper-case and vice - versa

  7. Program to copy the contents of one array to another Without Shared Memory

  8. Program that accepts characters in lower case, stores them in an array and prints them in the upper-case

  9. PROGRAM USING WHILE LOOP TO DO SUM OF DIGITS OF THE NUMBER. NO IS - 12345 ANS IS 15

  10. Program to evaluate fibonacci series and checking whether or not it is fibonacci number

  11. Program to generate random numbers

  12. Program to merge 2 sorted array in a single array

  13. Program to display progress bar as displayed in windows system

  14. Program to create a singly linked list of numbers using threads

  15. PROGRAM USNG WHILE LOOP TO REVERSE THE DIGITS OF THE NUMBER NO IS - 12345 ANSWER IS 54321

  16. Program to copy the contents of one array to another using shared memory

  17. Program of histogram for given data element

  18. Program to create processes using fork() and check different states of zombie process

  19. PROGRAM FOR SECANT METHOD OF PARTICULAR EQUATION IS log(x)-cos(x)

  20. Program to read two positive numbers and interchange its values using +(add) and -(sub) operators and without using any extra variable

 
 
Didn't find what you were looking for? Find more on PROGRAM FOR COPY THE ARRAY ELEMENETS USING N NO.OF PROCESSES