当前位置:网站首页>System programming summary
System programming summary
2022-06-30 04:25:00 【Tra220123】
One 、 File lock
1. When multiple processes have read and write access to the same file , In order to ensure the integrity of the data , Sometimes you need to lock files .
adopt fcntl() Lock and unlock files .
① For write locks (F_WRLCK An exclusive lock ), Only one process can have an exclusive lock on any particular area of the file .
② For reading locks (F_RDLCK Shared lock ), Many different processes can have shared locks on the same area of a file at the same time .
③ To have a shared lock , The file must be read or read / Write mode start . As long as any process has a shared lock , Then other processes can no longer obtain exclusive locks .
readlock.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void) {
int fd = open("./hello.txt", O_RDONLY);
if (fd < 0) {
perror("open");
exit(1);
}
struct stat sta;
fstat(fd, &sta);
struct flock lock;
lock.l_type = F_RDLCK;
lock.l_pid = getpid();
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = sta.st_size;
printf("pid : %d ", lock.l_pid);
if (fcntl(fd, F_SETLK, &lock) < 0) {
perror("fcntl");
exit(1);
} else {
printf("add read lock suceessfully\n");
}
sleep(10);
close(fd);
return 0;
}writelock.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void) {
int fd = open("./hello.txt", O_WRONLY);
if (fd < 0) {
perror("open");
exit(1);
}
struct stat sta;
fstat(fd, &sta);
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_pid = getpid();
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = sta.st_size;
printf("pid:%d ", lock.l_pid);
while (fcntl(fd, F_SETLK, &lock) < 0) {
perror("fcntl");
struct flock lock_1;
lock_1 = lock;
lock_1.l_type = F_WRLCK;
fcntl(fd, F_GETLK, &lock_1);
switch (lock_1.l_type)
{
case F_UNLCK:
printf("get no lock\n");
break;
case F_RDLCK:
printf("get read lock of pid = %d\n", lock_1.l_pid);
break;
case F_WRLCK:
printf("get write lock of pid = %d\n", lock_1.l_pid);
break;
}
sleep(1);
}
printf("set write lock successfully\n");
getchar();
close(fd);
return 0;
}Two 、 terminal
1. Concept :
After logging into the system through the terminal, the user gets a shell process , This terminal is called shell Process control process ,fork Will be copied PCB Information in , Therefore, from shell The control terminal of other processes started by the process .
By default ( No redirection ), Standard input for each process , Both standard output and standard error output point to the control terminal , The process reads from standard input ( Read the user's keyboard input ), The process writes to standard output or standard error output ( Output to the monitor ).
Take the output queue as an example , The characters input from the keyboard are filtered into the input queue by the line rules , The user program reads characters from the queue in first in first out order , In general , When the input queue is full, the input characters will be lost , At the same time, the system will ring loudly .
The terminal can be configured to echo (Echo) Pattern , In this mode , Each character in the input queue is sent to both the user program and the output queue , So when you enter characters on the command line , This character can not only be read by the program , You can also see the echo of this character on the screen at the same time .

2. The number of network terminal or graphics terminal windows is unlimited
Realized by pseudo terminal , A pseudo terminal has a master device (PTY Master) And a slave device (PTY Slave) form .
① The main device is conceptually equivalent to a keyboard and display , It's a kernel module , It is not the user but another process that operates it .
② The underlying driver of the slave device accesses the master device instead of the hardware .
3. View the device file name corresponding to the terminal

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void) {
int fd = open("./open.txt", O_WRONLY);
printf("fd %d:%s\n", 0, ttyname(0));
printf("fd %d:%s\n", 1, ttyname(1));
printf("fd %d:%s\n", 2, ttyname(2));
printf("fd %d:%s\n", fd, ttyname(fd));
return 0;
}
4. Homework
shell It is not the process but the job that is controlled by the front and background (Job) Or process group .
A foreground job is made up of multiple processes , A background job can also be composed of multiple processes .
① Operation control :shell It can run one foreground job and any number of background jobs at the same time .

Control the same process group as the terminal , Belong to the same session Session.
②. Compare the output of the following command


5. Daemon
① Concept :
Linux Many system service processes will be started when the system starts , These system service processes have no control terminals , You can't directly interact with users .
Other processes are created when the user logs in or runs the program , Terminate at the end of the run or when the user logs off , However, the system service process is not affected by user login and logout , It's running all the time .
The most critical step in creating a daemon is to call setsid Function to create a new session, And called session leader.

When the function call succeeds, it returns the newly created session Of id( The current process id), Error return -1.
Be careful : Before calling this function , The current process is not allowed to be part of a process group leader, Otherwise return to -1.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid) {
exit(0);
}
pid_t nsid = setsid();
printf("new session id is %d\n", nsid);
if (chdir("/") < 0) {
perror("chdir");
exit(1);
}
close(0);
open("/dev/null", O_RDWR);
dup2(0, 1);
dup2(0, 2);
while (1) {
sleep(1);
}
return 0;
}边栏推荐
- 股票利益【非dp】
- Qt 6.3.1Conan软件包发布
- Titanic(POJ2361)
- FortiGate firewall and Aruze cloud tunnel interruption
- Detailed explanation of data link layer
- Error in conditional filter (if) syntax in sum function in SQL Server2005
- base64.c
- The same node code will cause duplicate data
- FortiGate firewall configuration log uploading regularly
- FortiGate firewall configuration link detection link monitor and status query
猜你喜欢

Myrpc version 3

How to solve the problem of link hyperlinks when trying to link the database?

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里

Use of thread pool

什么是光耦电路,实际使用中应该注意些什么?

在大厂外包呆了三年,颠覆了我的认知!

Share an example of a simple MapReduce method using a virtual machine

Mongodb learning

Threejs realizes the simulation of river, surface flow, pipe flow and sea surface

Redis cache avalanche, breakdown and penetration
随机推荐
Myrpc version 0
Huawei cloud native - data development and datafactory
Pig-Latin (UVA492)
SQL追加字段
Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
Read / write lock example
Myrpc version 6
Default value of JS parameter
El upload Upload file (Manual upload, Automatic upload, upload progress)
Thinkphp5 implements import function
Splicing strings with custom functions
el-upload上傳文件(手動上傳,自動上傳,上傳進度)
Use of thread pool
Find the interface and add parameters to the form
JS deconstruction assignment
Collinearity problem
Array of small C
JS generator
el-upload上传文件(手动上传,自动上传,上传进度)
OneNote production schedule