loopmain.c
来自「《UNIX进程间通信》中的代码」· C语言 代码 · 共 39 行
C
39 行
#include "unpipc.h"#define SEQFILE "seqno" /* filename */void my_lock(int), my_unlock(int);intmain(int argc, char **argv){ int fd; long i, nloop, seqno; ssize_t n; char line[MAXLINE + 1]; if (argc != 2) err_quit("usage: loopmain <loopcount>"); nloop = atol(argv[1]); fd = Open(SEQFILE, O_RDWR, FILE_MODE); for (i = 0; i < nloop; i++) { my_lock(fd); /* lock the file */ Lseek(fd, 0L, SEEK_SET); /* rewind before read */ n = Read(fd, line, MAXLINE); line[n] = '\0'; /* null terminate for sscanf */ n = sscanf(line, "%ld\n", &seqno); seqno++; /* increment sequence number */ snprintf(line, sizeof(line), "%ld\n", seqno); Lseek(fd, 0L, SEEK_SET); /* rewind before write */ Write(fd, line, strlen(line)); my_unlock(fd); /* unlock the file */ } exit(0);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?