当前位置:网站首页>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;
}边栏推荐
- FortiGate firewall filters the specified session and cleans it up
- If you encounter problems when using spark for the first time, please ask for help
- JS import and export
- Blocking queue example
- AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
- lego_ Reading and summary of loam code
- 进程间通信之匿名管道
- Machine learning notes
- Detailed explanation of data link layer
- Unity 在編輯器中輸入字符串時,轉義字符的輸入
猜你喜欢

Machine learning notes

Educoder group purchase suspension box page production

iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务

El upload Upload file (Manual upload, Automatic upload, upload progress)

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

MySQL DDL change

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

Day 10 data saving and loading

Myrpc version 1

I get n offers in two months. I don't have any difficult interviewers here
随机推荐
Indefinite parameters of JS function
小C的数组(array)
JS proxy
Mongodb learning
Pig-Latin (UVA492)
Qt 6.3.1Conan软件包发布
Read / write lock example
An error occurs when sqlyog imports the database. Please help solve it!
OneNote software
Myrpc version 6
Fair lock and unfair lock
Named pipes for interprocess communication
Collinearity problem
JS reflect
What is an optocoupler circuit and what should be paid attention to in actual use?
Myrpc version 5
Intern method of string
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
errno和perror
The same node code will cause duplicate data