C语言实现write命令.并基于linux文件系统和who命令的c语言实现方法实现不同用户间的通信
时间: 2024-02-21 21:01:33 浏览: 119
实现write命令的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s user message\n", argv[0]);
exit(EXIT_FAILURE);
}
char *user = argv[1];
char *msg = argv[2];
char *pipe_name = malloc(strlen(user) + 6);
sprintf(pipe_name, "/tmp/%s.pipe", user);
if (mkfifo(pipe_name, 0666) == -1) {
if (errno != EEXIST) {
fprintf(stderr, "Failed to create pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
int fd = open(pipe_name, O_WRONLY);
if (fd == -1) {
fprintf(stderr, "Failed to open pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (write(fd, msg, strlen(msg)) == -1) {
fprintf(stderr, "Failed to write to pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
free(pipe_name);
exit(EXIT_SUCCESS);
}
```
基于Linux文件系统和who命令的C语言实现方法实现不同用户间的通信:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX_USERS 10
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s user\n", argv[0]);
exit(EXIT_FAILURE);
}
char *user = argv[1];
char *pipe_name = malloc(strlen(user) + 6);
sprintf(pipe_name, "/tmp/%s.pipe", user);
if (mkfifo(pipe_name, 0666) == -1) {
if (errno != EEXIST) {
fprintf(stderr, "Failed to create pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
int fds[MAX_USERS];
for (int i = 0; i < MAX_USERS; i++) {
if (i == getpid() % MAX_USERS) {
fds[i] = -1; // skip own pipe
continue;
}
char *other_user = malloc(10);
sprintf(other_user, "user%d", i);
char *other_pipe_name = malloc(strlen(other_user) + 6);
sprintf(other_pipe_name, "/tmp/%s.pipe", other_user);
fds[i] = open(other_pipe_name, O_WRONLY | O_NONBLOCK); // open non-blocking
if (fds[i] == -1) {
if (errno != ENXIO) { // ignore non-existent pipes
fprintf(stderr, "Failed to open pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
free(other_user);
free(other_pipe_name);
}
char buf[1024];
int read_fd = open(pipe_name, O_RDONLY | O_NONBLOCK); // open non-blocking
if (read_fd == -1) {
fprintf(stderr, "Failed to open read pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
while (1) {
int n = read(read_fd, buf, sizeof(buf));
if (n == -1) {
if (errno == EAGAIN) {
usleep(100000); // sleep for 100ms if no data available
continue;
} else {
fprintf(stderr, "Failed to read from pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
} else if (n == 0) {
break; // pipe closed
} else {
buf[n] = '\0';
printf("%s", buf);
for (int i = 0; i < MAX_USERS; i++) {
if (fds[i] != -1) {
if (write(fds[i], buf, n) == -1) {
fprintf(stderr, "Failed to write to pipe: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
}
}
}
close(read_fd);
free(pipe_name);
exit(EXIT_SUCCESS);
}
```
代码使用了非阻塞方式打开管道,以便在没有数据可读/写时不会阻塞。在实际应用中,应该使用多线程或异步IO等方法来避免阻塞。
阅读全文