Array, parent-child process, daemon

Source: Internet
Author: User
Tags exit in openlog syslog system log

2015.1.25
Sunday, Cloudy

A total of m rows n column elements in a two-dimensional array a
There are i*n+j elements from a[0][0] to A[i][j]
P represents the address of row No. 0 of line No. 0, so the address of element A[i][j] is P + i*n+j

A[I][J] = = P[i*n+j] = = * (p + i*n+j)

The system call functions for getting the current process PID and Ppid in Linux are Getpid () and Getppid (), which can be written to a log backup after obtaining the current PID and Ppid!

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main ()
{
printf ("The PID of this process is%d\n", getpid ());
printf ("The ppid of this process is%d\n", getppid ());

}
The above program can be cross-compiled to download to the target board to run!

The parent process calls the fork () function to create a child process, the parent process returns the PID of the child process, and the child process returns a value of 0;
The fork () function is expensive and cannot be used multiple times.

The EXEC function family provides a way to start another process execution in the process, with a total of 6 member functions, which must be used when using the EXEC function
To add an error-judgment statement. exec is easy to perform failure.

Daemon: Daemon, start running from execution until the system shuts down!

To write a daemon:

1. Create a child process and then the parent process exits!
PID = fork ();
if (PID > 0)
{
Exit (0);/* Parent Process exits */
}

2. Create a new session in a subprocess: an important step, significant! Setsid ();

3. Change the current directory to the root directory: Leave the child process out of the parent process's file working directory.

4. Reset file Permission mask: Leave the child process out of the file permission mask of the parent process.

such as: 050 It blocks the file group owner's readable and executable permissions. Implemented with function umask ()
The usual method of use is: Umask (0)

5. Close the file descriptor

Create a full instance of a daemon: Create a daemon, and then let the process write a sentence to the log file/tmp/daemon.log every 10S.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcn1.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main ()
{
pid_t pid;
int I, FD;
Char *buf = "This is a daemon\n";

PID = fork (); The first step
if (PID < 0)
{
printf ("Error fork\n");
Exit (1);
}
else if (PID > 0)
{
Exit (0); Parent process exits
}

Setsid (); Step Two
ChDir ("/"); Step Three
Umask (0); Fourth Step
for (i = 0; i < getdtablesize (); i++) Fifth step
{
Close (i);
}

/* When the daemon is created, the following begins to formally enter the daemon work */
while (1)
{
if (FD = open ("/tmp/daemon.log", o_creat| owronly| o_append.0600)) <0)
{
printf ("Open File error\n");
Exit (1);
}
Write (Fd,buf,strlen (BUF) + 1);
Close (FD);
Sleep (10);
}
Exit (0);
}

Daemon Error Handling:

GDB cannot debug the daemon because it is out of the terminal, and a common approach is to use the Syslog service to
The error information in the program is entered into the system log file;

The mechanism is related to three syslog functions: Openlog (); syslog (); Closelog ();
Rewrite the above program with the Syslog service;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcn1.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main ()
{
pid_t pid;
int I, FD;
Char *buf = "This is a daemon\n";

PID = fork (); The first step
if (PID < 0)
{
printf ("Error fork\n");
Exit (1);
}
else if (PID > 0)
{
Exit (0); Parent process exits
}

Openlog ("Daemon_syslog", Log_pid,log_daemon);

if (SID = Setsid () < 0), step two
{
Syslog (log_err,%s\n "," Setsid ");
Exit (1);
}

if (SID = ChDir ("/") < 0), step three
{
Syslog (log_err,%s\n "," chdir ");
Exit (1);
}
Umask (0); Fourth Step
for (i = 0; i < getdtablesize (); i++) Fifth step
{
Close (i);
}

/* When the daemon is created, the following begins to formally enter the daemon work */
while (1)
{
if (FD = open ("/tmp/daemon.log", o_creat| owronly| o_append.0600)) <0)
{
Syslog (Log_err, "open");
Exit (1);
}
Write (Fd,buf,strlen (BUF) + 1);
Close (FD);
Sleep (10);
}
Closelog ();
Exit (0);
}

Experimental content:
There are three processes, one of which is the parent process, and the remaining two are child processes of the parent process. One of the child processes runs
"Ls-l" instruction, another process pauses 5 seconds after the exception exits, the parent process first in a blocking way to wait for the end of the first child process,
Then the non-blocking way waits for the exit of the other process to be collected to the end of the second child process, and the parent process returns!


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main (void)
{
pid_t Child1, child2, child;

/* Create two sub-processes */
Child1 = Fock ();
Child2 = Fock ();

/* Error handling for child Process 1 */
if (child1 = =-1)
{
printf ("Child1 fork error\n");
Exit (1);
}
else if (child1 = = 0)/* call EXECLP () function in child process 1 */
{
printf ("In Child1:execute ' ls-l ' \ n");
if (EXECLP ("ls", "ls", "1", NULL) < 0)
{
printf ("Child1 execlp error\n");
}
}

if (child2 = =-1) sub-process 2 error handling
{
printf ("Child2 fork error\n");
Exit (1);
}
else if (child2 = = 0) pauses it in sub-process 2 for 5 seconds
{
printf ("In Child2:sleep for 5 seconds and then exit\n");
Sleep (5);
Exit (0);
}
else waits for two child processes to exit in the parent process
{
printf ("in Father Process:\n");
Child = Waitpid (child1,null,0); Blocking wait
if (child = = child1)
{
printf ("Get Child1 exit code\n");
}
Else
{
printf ("Error occured!\n");
}

Do
{
Child = Waitpid (Child2,null,wnohang); Non-blocking is waiting
if (child = = 0)
{
printf ("The child2 process has not exited!\n");
Sleep (1);
}
}while (Child = = 0);

if (child = = child2)
{
printf ("Get child2 exit code\n");
}
Else
{
printf ("Error occured!\n");
}
}
Exit (0);
}

Experimental content:
First, a daemon is established, and then a new child process is created in the daemon, which pauses for 10 seconds,
Then automatically exits, and the daemon collects messages that the child process exits, where the child process and daemon exit messages are
The system log file is output, and after the child process exits, the daemon cycles and pauses for a period of 10 seconds.


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>

int main (void)
{
pid_t Child1, Child2;
int i;
/* Create Child process */
Child1 = fork ();
if (child1 = = 1)
{
Perror ("Child1 fork");
Exit (1);
}

else if (child1 > 0)
{
Exit (0); Parent process exits
}

Openlog ("Daemon_proc_info", Log_pid,log_daemon);
/* The following steps are a general step to writing the daemon */
Setsid ();
ChDir ("/");
Umask (0);
for (i = 0; i < getdtablesize (); i++)
{
chose (i);
}

Child2 = fork (); Create process 2
if (child2 = = 1)
{
Perror ("Child2 fork");
Exit (1);
}
Else if (child2 = = 0) enters process 2
{
Syslog (log_info, "Child2 'll Sleep for 10s"); writes a string in the log
sleep;
Syslog (Log_info, "child2 is going to exit!"):
exit (0);
}
Else
{process child1

Waitpid (child2,null,0);
Syslog (Log_info, "Child1 noticed that child2 have exitde");
Closelog (); Turn off log service
while (1)
{
sleep];
}
}

}


*************************************************************************************************************** *******************************************
*************************************************************************************************************** *******************************************
*************************************************************************************************************** *******************************************

Array, parent-child process, daemon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.