Вы находитесь на странице: 1из 8

Shmget():

In order to create a new message queue, or access an existing queue, the shmget() system
call is used.

1.the key for the segment.

2.the size of the segment.

3.create/use flag.

-The first argument to shmget() is the key value . key_t, It is the numeric key to be assigned
to the returned shared memory segment. This key value is then compared to existing key
values that exist within the kernel for other shared memory segments.

-size of the segment is the size of the requested shared memory.

-The purpose of flag is to specify the way that the shared memory will be used.

-IPC_CREAT

Create the segment if it doesn't already exist in the kernel.

-The key value and requested segment size (in bytes) are passed as arguments.

-If shmget() can successfully get the requested shared memory, its function value is a non-
negative integer, the shared memory ID; otherwise, the function value is negative.

shmat():attaches the shared memory segment identified by shmid to the address space of
the calling process. The attaching address is specified by shmaddr.

Parameters:

shmid

Is a unique positive integer created by a shmget() system call and associated with a
segment of shared memory.
shmaddr

Points to the desired address of the shared memory segment.

-If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the
segment.i.e If shmaddr is a NULL pointer, the segment is attached at the first available
address as selected by the system.

shmflg

Specifies a set of flags that indicate the specific shared memory conditions and options to
implement.

-memset -fill memory with a constant byte

Parameters

// ptr ==> Starting address of memory to be filled

// x ==> Value to be filled

// n ==> Number of bytes to be filled starting

// from ptr to be filled

void *memset(void *ptr, int x, size_t n);


#include <sys/types.h>

#include <stdio.h>

#include <unistd.h>

int main()

pid t pid, pid1;

/* fork a child process */

pid = fork();

if (pid < 0)

/* error occurred */

fprintf(stderr, "Fork Failed");

return 1;

else if (pid == 0)

/* child process */

pid1 = getpid();

printf("child: pid = %d",pid); /* A */

printf("child: pid1 = %d",pid1); /* B */

else

/* parent process */

pid1 = getpid();

printf("parent: pid = %d",pid); /* C */


printf("parent: pid1 = %d",pid1); /* D */

wait(NULL);

return 0;

}
#include <stdio.h>

#include <unistd.h>

int main()

/* fork a child process */

fork();

/* fork another child process */

fork();

/* and fork another */

fork();

return 0;

}
#include <sys/types.h>

#include <stdio.h>

#include <unistd.h>

#include <sys/wait.h>

int value = 5;

int main()

pid t pid;

pid = fork();

if (pid == 0)

/* child process */

value += 15;

return 0;

else if (pid > 0)

/* parent process */

wait(NULL);

printf("PARENT: value = %d",value); /* LINE A */

return 0;
}

#include <sys/types.h>

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/wait.h>

int value = 5;

int main()

pid_t pid;

pid = fork();

if (pid == 0)

{ /* child process */

int value2= value+1;

printf("\nvalue =%d",value2);

return 0;

else if (pid > 0)

{ /* parent process */

wait(NULL);

printf("\nPARENT: value = %d",value); /* LINE A */


return 0;

Вам также может понравиться