Thread Pool shared memory + semaphore
Deepfuture @ deepfuture-laptop :~ /Private/mytest $ gcc-std = gnu99-o testshm. c
Testshm. c: In function 'main ':
Testshm. c: 38: warning: implicit declaration of function 'secet'
Testshm. c: 41: warning: implicit declaration of function 'delete'
Testshm. c: 41: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 45: warning: implicit declaration of function 'semctl'
Testshm. c: 48: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 51: warning: implicit declaration of function 'shmget'
Testshm. c: 54: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 57: warning: implicit declaration of function 'shmat'
Testshm. c: 60: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 63: warning: implicit declaration of function 'memset'
Testshm. c: 63: warning: incompatible implicit declaration of built-in function 'memset'
Testshm. c: 69: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 78: warning: implicit declaration of function 'strlen'
Testshm. c: 78: warning: incompatible implicit declaration of built-in function 'strlen'
Testshm. c: 85: warning: implicit declaration of function 'memcpy'
Testshm. c: 85: warning: incompatible implicit declaration of built-in function 'memcpy'
Testshm. c: 92: warning: implicit declaration of function 'semop'
Testshm. c: 95: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 119: warning: incompatible implicit declaration of built-in function 'strlen'
Testshm. c: 124: warning: incompatible implicit declaration of built-in function 'eg'
Testshm. c: 132: warning: implicit declaration of function 'wait'
Testshm. c: 134: warning: implicit declaration of function 'shmdt'
Testshm. c: 139: warning: implicit declaration of function 'shmctl'
Testshm. c: 142: warning: incompatible implicit declaration of built-in function 'eg'
Deepfuture @ deepfuture-laptop :~ /Private/mytest $./testshm
Deepfuture.javeye.com # line 1 $ deepfuture
Deepfuture.javeye.com # line 2 $ javaeye
Deepfuture.javeye.com # line 3 $ com
Deepfuture.javeye.com # line 4 $ myhaspl
Deepfuture.javeye.com # line 5 $ Q
Deepfuture.javeye.com # line 6 $
Exit ....
Deepfuture @ deepfuture-laptop :~ /Private/mytest $ cat abc.txt
Deepfuture
Javaeye
Com
Myhaspl
Deepfuture @ deepfuture-laptop :~ /Private/mytest $ good AI Park blog all content is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
C code
# Include
# Include
# Include
# Include
# Include
# Include
# Define MAXS (1024 + 1)
# Define BUFFERSIZE 200
# Define SEMID 251 // signal flag
# Define FILENAME "abc.txt"
# Define SHMKEY 241 // shared memory flag
# Define shmsize maxs // shared memory size
// The program completes receiving keyboard input by the parent process, and the sub-process stores the file FILENAME.
// Myhaspl
Int main (void ){
Char strbuf [MAXS];
Char buf [BUFFERSIZE];
Int sem_id;
Int shm_id;
Int pid;
Int rc, res;
Struct sembuf sem_op; // Signal Set Structure
Union semun sem_val; // semaphore Value
Char * cur;
FILE * myfile;
Char * shm_addr;
Int line = 1;
// Create a semaphores set, with only one semaphores myhaspl
Sem_id = semget (SEMID, 1, IPC_CREAT | 0600); // SEMID is a positive integer, which is public; 1 is the number of signal sets;
If (sem_id =-1 ){
Printf ("create sem error! \ N ");
Exit (1 );
}
// Semaphore Initialization
Sem_val.val = 0;
Rc = semctl (sem_id, 0, SETVAL, sem_val); // set the semaphore
If (rc =-1 ){
Printf ("initlize sem error! \ N ");
Exit (1 );
}
// Create shared memory
Shm_id = shmget (SHMKEY, SHMSIZE, IPC_CREAT | 0600); // The parameter is flag, size, and permission.
If (shm_id =-1 ){
Printf ("create shm error! \ N ");
Exit (1 );
}
// Attach shared memory. Connect to the shared memory myhaspl
Shm_addr = (char *) shmat (shm_id, NULL, 0); // return the shared memory address myhaspl
If (! Shm_addr ){
Printf ("shmat error! \ N ");
Exit (1 );
}
// Initialize data
Memset (shm_addr, '\ 0', MAXS );
Cur = shm_addr; // start address of the Current Character
// Create a process
Pid = fork ();
If (pid =-1 ){
Printf ("fork error! \ N ");
Exit (1 );
}
Else if (pid = 0) {// sub-process that receives keyboard input and writes the myhaspl line to the shared memory
Int isend = 0; // whether to end the input.
Printf ("\ ndeepfuture.javeye.com # line % d $", line); // customize the SHELL appearance used for keyboard input
While ((! Isend) & fgets (buf, BUFFERSIZE, stdin )! = NULL) {// read a row from shell
Line ++;
Printf ("\ ndeepfuture.javeye.com # line % d $", line); // customize the SHELL appearance used for keyboard input
If (buf [0] = 'q' & strlen (buf) <= 2) {// a single character Q indicates exiting the input
Isend ++; // exit the input
Printf ("\ n exit... \ n ");
}
Else
{// If it is not an exit command
// Write shared memory myhaspl
Memcpy (cur, buf, strlen (buf ));
Cur + = strlen (buf );
}
// Write a row to add a signal
Sem_op.sem_num = 0;
Sem_op.sem_op = 1;
Sem_op.sem_flg = 0;
Semop (sem_id, & sem_op, 1); // operation semaphore, each time + 1
}
* Cur =-1;
Exit (0 );
}
Else {// parent process, which reads character lines from the shared memory and writes the myhaspl File
While (1)
{
// Read a row to reduce the signal myhaspl
Sem_op.sem_num = 0;
Sem_op.sem_op =-1;
Sem_op.sem_flg = 0;
Semop (sem_id, & sem_op, 1); // operation semaphore, each time-1
// One row of myhaspl read shared memory
If (* cur) =-1) break; // The input ends.
Int I;
For (I = 0; * cur! = '\ N'; cur ++, I ++ ){
Buf [I] = * cur;
}
Cur ++;
Buf [I] = '\ n ';
Buf [++ I] = 0;
// Write an object
FILE * fp = fopen (FILENAME, "AB ");
Res = fwrite (buf, strlen (buf), 1, fp); // write a row to myhaspl. The length is strlen (buf) and the number is 1.
// Size_t fwrite (const void * ptr, size_t size, size_t nmemb, FILE * stream );
// Size is the size (in bytes) of each data to be written, and nmemb is the number of data to be written. Myhaspl
If (res =-1 ){
Perror ("write error on pipe \ n ");
Exit (1 );
}
Fclose (fp );
}
Wait (& pid); // wait until the sub-process ends, that is, the user input is complete.
// Detach the sharing process
If (shmdt (shm_addr) =-1 ){
Printf ("shmdt error! \ N ");
}
// Undo the shared memory. Any process can undo the shared memory as long as it has the permission. It does not have to be created.
Struct shmid_ds shm_desc;
If (shmctl (shm_id, IPC_RMID, & shm_desc) =-1 ){
Printf ("shmctl error! \ N ");
}
Exit (0 );
}
}