请帮我进行系统调用编程 ,要求如下使用系统调用open read write,完成一个C语言程序:该程序接收两个参数,参数1作为要读写的文件,参数2作为标志位,标识是读还是写。功能完成对参数1所示文件的读写,该文件的内容为 “学号 MYFILE”,其中学号填为自己的学号。 改造上面的程序,使用semaphore,并利用该程序生成2个进程,这两个进程写同一个文件,要求:a.互斥写,即只有一个进程写完后,才能让另一个进程写; b. 一个进程写入内容:“自己学号的后3位PROC1 MYFILE1”;另一个进程写入内容:“自己学号的后3位PROC2 MYFILE2” 请提交代码截图和运行结果截图
时间: 2024-01-22 20:20:26 浏览: 73
好的,首先是第一个要求的程序实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s filename flag\n", argv[0]);
exit(1);
}
int fd;
if (strcmp(argv[2], "r") == 0) {
fd = open(argv[1], O_RDONLY);
} else if (strcmp(argv[2], "w") == 0) {
fd = open(argv[1], O_WRONLY);
} else {
printf("Invalid flag: %s\n", argv[2]);
exit(1);
}
if (fd == -1) {
perror("open");
exit(1);
}
char buf[20];
if (read(fd, buf, 20) == -1) {
perror("read");
exit(1);
}
if (strcmp(argv[2], "r") == 0) {
printf("Content of file %s: %s\n", argv[1], buf);
} else {
char* my_file = malloc(sizeof(char) * 10);
sprintf(my_file, "MYFILE%d", atoi(argv[1]+6));
sprintf(buf, "%sPROC%d", argv[1]+7, getpid()%2+1);
if (write(fd, buf, strlen(buf)) == -1) {
perror("write");
exit(1);
}
printf("Content written to file %s: %s\n", my_file, buf);
free(my_file);
}
if (close(fd) == -1) {
perror("close");
exit(1);
}
return 0;
}
```
然后是使用 semaphore 实现的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <string.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
exit(1);
}
sem_t* sem_write;
sem_t* sem_read;
sem_t* sem_mutex;
int fd;
char* buf;
char* my_file;
sem_write = sem_open("write", O_CREAT, 0644, 1);
sem_read = sem_open("read", O_CREAT, 0644, 0);
sem_mutex = sem_open("mutex", O_CREAT, 0644, 1);
if (sem_write == SEM_FAILED || sem_read == SEM_FAILED || sem_mutex == SEM_FAILED) {
perror("sem_open");
exit(1);
}
fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
perror("open");
exit(1);
}
if (ftruncate(fd, 40) == -1) {
perror("ftruncate");
exit(1);
}
buf = mmap(NULL, 40, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(1);
}
my_file = malloc(sizeof(char) * 10);
sprintf(my_file, "MYFILE%d", getpid()%2+1);
int pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
} else if (pid == 0) {
// child process
while (1) {
sem_wait(sem_write);
sem_wait(sem_mutex);
sprintf(buf, "%sPROC1", argv[0]+7);
memcpy(buf+10, " ", 6);
memcpy(buf+16, my_file, strlen(my_file));
if (write(fd, buf, strlen(buf)) == -1) {
perror("write");
exit(1);
}
printf("Content written to file %s by child process: %s\n", my_file, buf);
sem_post(sem_mutex);
sem_post(sem_read);
}
} else {
// parent process
int i;
for (i = 0; i < 10; i++) {
sem_wait(sem_read);
sem_wait(sem_mutex);
sprintf(buf, "%sPROC2", argv[0]+7);
memcpy(buf+10, " ", 6);
memcpy(buf+16, my_file, strlen(my_file));
if (write(fd, buf, strlen(buf)) == -1) {
perror("write");
exit(1);
}
printf("Content written to file %s by parent process: %s\n", my_file, buf);
sem_post(sem_mutex);
sem_post(sem_write);
}
if (waitpid(pid, NULL, 0) == -1) {
perror("waitpid");
exit(1);
}
}
if (munmap(buf, 40) == -1) {
perror("munmap");
exit(1);
}
if (close(fd) == -1) {
perror("close");
exit(1);
}
if (sem_close(sem_write) == -1 || sem_close(sem_read) == -1 || sem_close(sem_mutex) == -1) {
perror("sem_close");
exit(1);
}
if (sem_unlink("write") == -1 || sem_unlink("read") == -1 || sem_unlink("mutex") == -1) {
perror("sem_unlink");
exit(1);
}
free(my_file);
return 0;
}
```
注意到以上程序需要在 Linux 系统上运行,且需要安装 GCC 编译器。如果你不确定自己的操作系统和环境,可以先询问一下。
阅读全文