Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of single process Control of C background Service Program in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of the single process control of the C background service program in Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Introduction

Usually the background server program must have one and only one process, so how to single process?

This example locks the / var/run/myserver.pid record pid file through the flock function.

If the locking is not normal, it means that the backend service process is already running, and then directly report an error to exit.

If the lock is successful, the background service process is not running, and the process can be enabled normally.

Background server single process control

Code

# include # define PID_BUF_LEN (20) # define RUN_PID_FILE "/ var/run/myserver.pid" / / Service process single instance run / / returned value: 1 int fd-running, 0 int fd-not running,-1 int fd-error int server_is_running () {int fd = open (RUN_PID_FILE, O_WRONLY | O_CREAT) If (fd < 0) {printf ("open run pid err (% d)!% s\ n", errno, RUN_PID_FILE); return-1;} / lock / / LOCK_SH establish a shared lock. Multiple processes can lock the same file at the same time. / / LOCK_EX establishes a mutex lock. A file has only one mutex lock at the same time. If (flock (fd, LOCK_EX | LOCK_NB) =-1) {/ / if the service is not locked, the service is running and printf is locked ("server is runing now! Errno=%d\ n ", errno); close (fd); return 1;} / / lock successfully, which proves that the service is not running / / file handle is not closed, and do not unlock / / process exit, printf is automatically unlocked (" myserver is not running! Begin to run. Pid=%ld\ n ", (long) getpid (); char pid_ buf [pid _ BUF_LEN] = {0}; snprintf (pid_buf, sizeof (pid_buf)-1,"% ld\ n ", (long) getpid ()); / / write process pid to / var/run/myserver.pid file write (fd, pid_buf, strlen (pid_buf)); return 0 } int main (void) {/ / process single instance run detection if (0! = server_is_running ()) {printf ("myserver process is running!! Current process will exit!\ n "); return-1;} while (1) {printf (" myserver doing...\ n "); sleep (2);} return 0;}

Running result

If you run the program, you know that the process pid is 6965

[root@lincoding singleprocess] #. / myserver server is not running! Begin to run. Pid=6965myserver doing... Myserver doing... Myserver doing... Myserver doing... Myserver doing... Myserver doing... Myserver doing... Myserver doing...

/ var/run/myserver.pid also records the pid number of this process. Ps auxf | grep myserver knows that the mysever process is running all the time.

[root@lincoding singleprocess] # cat / var/run/myserver.pid 6965 [root@lincoding singleprocess] # [root@lincoding singleprocess] # ps auxf | grep myserverroot 6965 0.0 3924 460 pts/0 S+ 00:32 0:00 |\ _. / myserverroot 9976 0.0 103256 856 pts/1 S+ 00:35\ _ grep myserver [root@lincoding singleprocess] #

At this point, if you run the myserver program again, you will report an error and exit, because it is detected that the myserver program is already running and cannot start another process, thus achieving the single process control of the background service program.

[root@lincoding singleprocess] #. / myserver server is runing now! Errno=11myserver process is running! Current process will exit! This is the end of this article on "sample analysis of single process control of C background service programs in Linux". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report