系统调用编程: 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-12 20:06:02 浏览: 35
1. 使用系统调用完成文件读写
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s file flag\n", argv[0]);
return -1;
}
int fd;
char buf[1024];
int len;
if (strcmp(argv[2], "r") == 0) {
// 读文件
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
printf("Open file failed!\n");
return -1;
}
len = read(fd, buf, sizeof(buf));
printf("Read from file: %s\n", buf);
close(fd);
} else if (strcmp(argv[2], "w") == 0) {
// 写文件
char *content = malloc(strlen(argv[1]) + 14);
sprintf(content, "%s MYFILE\n", argv[1]);
fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
printf("Open file failed!\n");
return -1;
}
len = write(fd, content, strlen(content));
printf("Write to file: %s", content);
free(content);
close(fd);
} else {
printf("Invalid flag!\n");
return -1;
}
return 0;
}
```
该程序接收两个参数,参数1作为要读写的文件,参数2作为标志位,标识是读还是写。在读文件时,使用 `open` 函数打开文件,使用 `read` 函数读取文件内容,最后输出读取结果。在写文件时,首先将要写入的内容格式化为字符串,然后使用 `open` 函数打开文件,使用 `write` 函数将字符串写入文件,最后输出写入结果。
2. 使用semaphore实现两个进程的互斥写
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
sem_t sem;
void write_file(char *filename, char *content) {
int fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd < 0) {
printf("Open file %s failed!\n", filename);
return;
}
sem_wait(&sem); // 等待信号量
write(fd, content, strlen(content));
sem_post(&sem); // 发送信号量
close(fd);
}
void process1(char *filename, char *num) {
char *content = malloc(strlen(num) + 23);
sprintf(content, "%sPROC1 %s\n", num + strlen(num) - 3, filename);
write_file(filename, content);
free(content);
}
void process2(char *filename, char *num) {
char *content = malloc(strlen(num) + 23);
sprintf(content, "%sPROC2 %s\n", num + strlen(num) - 3, filename);
write_file(filename, content);
free(content);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s filename\n", argv[0]);
return -1;
}
sem_init(&sem, 0, 1); // 初始化信号量
char *num = "2022xxxx"; // 替换为自己的学号
pid_t pid;
pid = fork();
if (pid < 0) {
printf("Fork failed!\n");
return -1;
} else if (pid == 0) {
// 子进程1
process1(argv[1], num);
} else {
// 父进程
pid_t pid2;
pid2 = fork();
if (pid2 < 0) {
printf("Fork failed!\n");
return -1;
} else if (pid2 == 0) {
// 子进程2
process2(argv[1], num);
} else {
// 父进程
int status;
waitpid(pid, &status, 0);
waitpid(pid2, &status, 0);
}
}
sem_destroy(&sem); // 销毁信号量
return 0;
}
```
该程序使用 `semaphore` 实现两个进程的互斥写。首先定义了三个函数:`write_file` 用于写文件,`process1` 用于进程1的逻辑,`process2` 用于进程2的逻辑。在 `write_file` 函数中,先使用 `sem_wait` 等待信号量,然后写文件,最后使用 `sem_post` 发送信号量。
在 `process1` 和 `process2` 函数中,先生成要写入文件的内容,然后调用 `write_file` 函数写入文件。
在 `main` 函数中,首先初始化信号量,然后使用 `fork` 函数生成两个子进程,子进程1执行 `process1` 函数,子进程2执行 `process2` 函数,父进程等待两个子进程执行完毕后退出。最后销毁信号量。