1. 使用系统调用open read write,完成一个C语言程序:该程序接收两个参数,参数1作为要读写的文件,参数2作为标志位,标识是读还是写。功能完成对参数1所示文件的读写,该文件的内容为 “学号 MYFILE”,其中学号填为自己的学号。 2. 改造上面的程序,完成: i. 使用semaphore,并利用该程序生成2个进程,这两个进程写同一个文件,要求:a.互斥写,即只有一个进程写完后,才能让另一个进程写; b. 一个进程写入内容:“自己学号的后3位PROC1 MYFILE1”;另一个进程写入内容:“自己学号的后3位PROC2 MYFILE2 ii. 将上面程序的semaphore替换成strict alternation算法,完成上面两个进程的互斥。
时间: 2024-02-06 19:09:05 浏览: 27
1. 使用系统调用open read write,完成一个C语言程序:
```c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_BUF_SIZE 1024
int main(int argc, char *argv[]) {
int fd, flag, num;
char buf[MAX_BUF_SIZE];
if (argc != 3) {
fprintf(stderr, "Usage: %s filename r|w\n", argv[0]);
exit(1);
}
if ((fd = open(argv[1], O_CREAT | O_RDWR, 0644)) == -1) {
perror("open error");
exit(1);
}
flag = (argv[2][0] == 'r' ? O_RDONLY : O_WRONLY);
if (lseek(fd, 0, SEEK_END) == 0) {
sprintf(buf, "学号 %s", "MYFILE");
num = write(fd, buf, strlen(buf));
if (num < strlen(buf)) {
perror("write error");
exit(1);
}
}
lseek(fd, 0, SEEK_SET);
if (flag == O_RDONLY) {
num = read(fd, buf, MAX_BUF_SIZE);
if (num < 0) {
perror("read error");
exit(1);
}
write(STDOUT_FILENO, buf, num);
} else {
sprintf(buf, "%s%s%s", "学号 ", argv[1], "\n");
num = write(fd, buf, strlen(buf));
if (num < strlen(buf)) {
perror("write error");
exit(1);
}
}
close(fd);
return 0;
}
```
2. 改造上面的程序,完成:
i. 使用semaphore,并利用该程序生成2个进程,这两个进程写同一个文件,要求:
a.互斥写,即只有一个进程写完后,才能让另一个进程写;
b. 一个进程写入内容:“自己学号的后3位PROC1 MYFILE1”;另一个进程写入内容:“自己学号的后3位PROC2 MYFILE2”
ii. 将上面程序的semaphore替换成strict alternation算法,完成上面两个进程的互斥。
```c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/wait.h>
#define MAX_BUF_SIZE 1024
sem_t sem;
void process(int id, char *filename, int flag) {
int fd, num;
char buf[MAX_BUF_SIZE];
if ((fd = open(filename, O_CREAT | O_RDWR, 0644)) == -1) {
perror("open error");
exit(1);
}
sem_wait(&sem);
if (flag == O_RDONLY) {
num = read(fd, buf, MAX_BUF_SIZE);
if (num < 0) {
perror("read error");
exit(1);
}
write(STDOUT_FILENO, buf, num);
} else {
sprintf(buf, "%s%s%s", "学号 ", filename, "\n");
if (id == 1) {
sprintf(buf, "%s%s%s", "学号 ", filename, " PROC1 MYFILE1\n");
} else {
sprintf(buf, "%s%s%s", "学号 ", filename, " PROC2 MYFILE2\n");
}
num = write(fd, buf, strlen(buf));
if (num < strlen(buf)) {
perror("write error");
exit(1);
}
}
sem_post(&sem);
close(fd);
}
int main(int argc, char *argv[]) {
pid_t pid1, pid2;
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(1);
}
sem_init(&sem, 0, 1);
pid1 = fork();
if (pid1 == 0) {
process(1, argv[1], O_WRONLY);
exit(0);
}
pid2 = fork();
if (pid2 == 0) {
process(2, argv[1], O_WRONLY);
exit(0);
}
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
process(0, argv[1], O_RDONLY);
sem_destroy(&sem);
return 0;
}
```
```c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/wait.h>
#define MAX_BUF_SIZE 1024
sem_t sem1, sem2;
void process(int id, char *filename, int flag) {
int fd, num;
char buf[MAX_BUF_SIZE];
if ((fd = open(filename, O_CREAT | O_RDWR, 0644)) == -1) {
perror("open error");
exit(1);
}
if (id == 1) {
sem_wait(&sem1);
} else {
sem_wait(&sem2);
}
if (flag == O_RDONLY) {
num = read(fd, buf, MAX_BUF_SIZE);
if (num < 0) {
perror("read error");
exit(1);
}
write(STDOUT_FILENO, buf, num);
} else {
sprintf(buf, "%s%s%s", "学号 ", filename, "\n");
if (id == 1) {
sprintf(buf, "%s%s%s", "学号 ", filename, " PROC1 MYFILE1\n");
} else {
sprintf(buf, "%s%s%s", "学号 ", filename, " PROC2 MYFILE2\n");
}
num = write(fd, buf, strlen(buf));
if (num < strlen(buf)) {
perror("write error");
exit(1);
}
}
if (id == 1) {
sem_post(&sem2);
} else {
sem_post(&sem1);
}
close(fd);
}
int main(int argc, char *argv[]) {
pid_t pid1, pid2;
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(1);
}
sem_init(&sem1, 0, 1);
sem_init(&sem2, 0, 0);
pid1 = fork();
if (pid1 == 0) {
process(1, argv[1], O_WRONLY);
exit(0);
}
pid2 = fork();
if (pid2 == 0) {
process(2, argv[1], O_WRONLY);
exit(0);
}
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
process(0, argv[1], O_RDONLY);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)