Академический Документы
Профессиональный Документы
Культура Документы
In order to create a new message queue, or access an existing queue, the shmget() system
call is used.
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.
-The purpose of flag is to specify the way that the shared memory will be used.
-IPC_CREAT
-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
-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.
Parameters
#include <stdio.h>
#include <unistd.h>
int main()
pid = fork();
if (pid < 0)
/* error occurred */
return 1;
else if (pid == 0)
/* child process */
pid1 = getpid();
else
/* parent process */
pid1 = getpid();
wait(NULL);
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main()
fork();
fork();
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;
/* parent process */
wait(NULL);
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 */
printf("\nvalue =%d",value2);
return 0;
{ /* parent process */
wait(NULL);