这个程序需要建立多个thread,然后使用shared memory来进行数据共享。
以下是这个程序的要求说明。
This program takes 3 command line arguments; the first
two are expected to be non-negative integers, the third, the name of a file
(the file may or may not exist). We shall compile your program with
% cc -Wall -o q2 q2.c -lpthread
The program would be invoked as follows:
% q2 n m <fileName>
q2 should fork off n children, numbered 0 .. n-1.
Each child should append to the named file its number (in 0 .. n-1, i.e.
not its PID) followed by a newline character '\n' m times.
No other output should be produced.
The child processes should use shared memory to coordinate their
activities, so that the output proceeds in a strictly round-robin fashion,
starting with child 0. For example, the command
% q2 5 3 temp
should result in the following 30 bytes being appended to temp:
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
Use good programming throughout. The exit status/return value
of your program should indicate its success/failure.
You will need to use system calls like fork, wait/waitpid, shmget, etc.
Be sure that your program removes any shared memory segments it creates.
Just to make sure that this is done, whenever your program finishes, you
should give the command
% ipcs -m
to see the shared memory in the system. Then remove any that belong to
you by using the command
% ipcrm shm <shmid>
Then try
% ipcs -m
once again, to make sure that the remove command was successful.
We shall regularly be using ipcs to check for shared memory segments
belonging to students who are not current logged on and deducting marks
when we find them.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc, char ** argv)
{
/* declaring variables */
int n;
int m;
int identifier;
char * p;
int i, j;
char buf[100];
pid_t pid;
int status;
int num;
/* declaring functions */
char * strerror( int );
int strlen(char *);
/* create a pointer point to the file name */
char *filename;
/* checking the number of arguments */
if (argc != 4)
{
printf("Invalid number of arguments\n");
exit(1);
}
n = atoi(argv[1]);
m = atoi(argv[2]);
filename = argv[3];
/* cheching the first two arguments */
if (m < 0 || n < 0)
{
printf("Input negative number(s)\n");
} 
/* define file access mode */
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
/* open a file */
int pfd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_APPEND | O_TRUNC, mode);
if (pfd == -1)
{
printf("%s: %s.\n", filename, strerror(errno));
exit(1);
}
/* creating shared memory */
if ((identifier = shmget(IPC_PRIVATE, 1, SHM_R | SHM_W)) < 0)
{
perror("Cannot created shared memory.");
exit(1);
}
/* attach the shared memory */
p = shmat(identifier, NULL, 0);
/* initialize the variable in shared memory */
*p = 0;
/* create n child processes */
for (i = 0; i < n; i++)
{
switch(pid = fork())
{
case -1: /* bad fork */
perror("Fork error");
exit(1);
case 0: /* child process */
if ((p = shmat(identifier, NULL, 0)) == (char *)-1)
{
perror("Child cannot attach to shared memory.");
exit(1);
}
/* redirect the standard output to file */
dup2(pfd, STDOUT_FILENO);
/* close file description */
close(pfd);
/* each process has to write the file m times */
for (j = 0; j < m; j++)
{
while (*p != i) /* waiting for other processes to run */
;
/* writing to the file */
num = i;
sprintf(buf, "%d\n", num);
write(1, buf, strlen(buf));
/* set *p to 0 */
if (*p == n - 1)
*p = 0;
else /* increment *p */
(*p)++;
}
/* detach to shared memory */
shmdt(p);
exit(0);
default: /* parent process do nothing */
;
}
}
/* delete shared memory */
if (shmctl(identifier, IPC_RMID, 0) < 0)
{
perror("Cannot remove shared memory.");
exit(1);
}
/* waiting for the last forked child process to finish */
waitpid(pid, &status, 0);
return 0;
}


浙公网安备 33010602011771号