11-Week Study report

Source: Internet
Author: User

exec1.c

#include <stdio.h>

#include <unistd.h>

int main ()

{

Char *arglist[3];

ARGLIST[0] = "ls";

ARGLIST[1] = "-L";

ARGLIST[2] = 0;//null

printf ("* * * ~ to exec ls-l\n");

EXECVP ("ls", arglist);//The name of the file that matches the parameter is found in the directory referred to by the PATH environment variable , the file is executed after it is located, and the second parameter argv passed to the file to be executed. If the execution succeeds the function does not return, the execution fails and returns 1, the reason for the failure is stored in errno

printf ("* * * * ls is done. Bye ");

return 0;

}

Exec2.c

#include <stdio.h>

#include <unistd.h>

int main ()

{

Char *arglist[3];

ARGLIST[0] = "ls";

ARGLIST[1] = "-L";

ARGLIST[2] = 0;

printf ("* * * ~ to exec ls-l\n");

EXECVP (Arglist[0], arglist);

printf ("* * * * ls is done. Bye\n ");

}//EXEC1 is the ls,exec2 Direct arglist[0]

Exec3.c#include <stdio.h> #include <unistd.h> int main () {char *arglist[3];char *myenv[3];myenv[0] = "Path=: /bin: "; myenv[1] = NULL; ARGLIST[0] = "ls"; arglist[1] = "-L"; arglist[2] = 0;p rintf ("* * * About to exec ls-l\n");//Execv ("/bin/ls", arglist); EXECVP ("ls", arglist);//EXECVPE ("ls", arglist, myenv);

EXECLP ("ls", "ls", "-l", NULL);PATHThe environment variable refers to the directory in which the lookup matches the parametersfilefile name, executes the file after it is found, and then considers the second parameter as the file'sArgv[0],argv[1] ..., the last argument must be with a null pointer(NULL)end. If you use Constants0to represent a null pointer, it must be cast to a character pointer, otherwise it will be interpreted as an shaping parameter, if the length of an shaping number ischar *the length is different, thenexecThe actual parameters of the function will be faulted. If the function call succeeds,the process's own execution code becomes the code of the loader., EXECLP ()the code behind will not be executed..return value: If the execution succeeds the function will not return, and the execution failure will return directly-1, the reason for failure exists inerrnothe.

printf ("* * * * ls is done. Bye\n ");}

forkdemo1.c

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main ()

{

int ret_from_fork, mypid;

Mypid = Getpid ();

printf ("Before:my pid is%d\n", mypid);

Ret_from_fork = fork ();

Sleep (1);

printf ("After:my pid is%d, fork () said%d\n",

Getpid (), ret_from_fork);//This Code first prints the process pid, then calls the fork function to generate the child process, sleeps one second and prints the process ID again , the parent process prints the child process pid, and the child process returns 0.

return 0;

}

Forkdemo2.c

#include <stdio.h>

#include <unistd.h>

int main ()

{

printf ("Before:my pid is%d\n", getpid ());

Fork ();

Fork ();//This code calls two fork, altogether produces four sub-processes, so it prints four aftre output.

printf ("Aftre:my pid is%d\n", getpid ());

return 0;

}

Forkdemo3.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main ()

{

int FORK_RV;

printf ("Before:my pid is%d\n", getpid ());

FORK_RV = fork (); /* Create new process */

if (FORK_RV = =-1)/* Check for Error */

Perror ("fork");

else if (Fork_rv = = 0) {

printf ("I am the Child.") My pid=%d\n ", Getpid ());

Exit (0);

}

else{

printf ("I am the parent.") My child is%d\n ", FORK_RV);//fork produces a child process, the parent process returns the child process pid, not 0, so the output of the parent process's statement, the subprocess returns 0, so the word will output the sub-process.

Exit (0);

}

return 0;

}

Forkdemo4.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main ()

{

int FORK_RV;

printf ("Before:my pid is%d\n", getpid ());

FORK_RV = fork (); /* Create new process */

if (FORK_RV = =-1)/* Check for Error */

Perror ("fork");

else if (Fork_rv = = 0) {

printf ("I am the Child.") My pid=%d\n ", Getpid ());

printf ("Parent pid=%d, my pid=%d\n", Getppid (), Getpid ());

Exit (0);

}

else{

printf ("I am the parent.") My child is%d\n ", FORK_RV);//print the process pidFirst,then Fork to create the sub-process, the parent process returns the child process pid, So output the parent sentence, hibernate for 10 seconds, and the child process returns 0, so the output of the children and the following sentence.

Sleep (10);

Exit (0);

}

return 0;

}

Forkgdb.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int gi=0;

int main ()

{

int li=0;

static int si=0;

int i=0;

pid_t pid = fork ();

if (PID = =-1) {

Exit (-1);

}

else if (PID = = 0) {

for (i=0; i<5; i++) {

printf ("Child li:%d\n", li++);

Sleep (1);

printf ("Child gi:%d\n", gi++);

printf ("Child si:%d\n", si++);

}

Exit (0);

}

else{

for (i=0; i<5; i++) {

printf ("Parent li:%d\n", li++);

printf ("Parent gi:%d\n", gi++);

Sleep (1);

printf ("Parent si:%d\n", si++);

}

The main difference is that the parent process prints two sentences first, then sleeps for a second, then prints a sentence, the child process first prints a sentence, then sleeps for a second, and then prints two sentences. And these two threads are concurrent, so you can see that one thread sleeps in the second, another thread is executing, and the threads are independent of each other.

Exit (0);

}

return 0;

}

Testbuf.c

#include <stdio.h>

#include <stdlib.h>

int main ()

{

printf ("Hello");//output Hello, then newline.

Fflush (stdout);

while (1);

}

Testbuf3.c

#include <stdio.h>

int main ()

{

fprintf (stdout, "1234", 5);//content format output to standard error, output stream

fprintf (stderr, "ABCD", 4);

}

Testpid.c

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main ()

{

printf ("My PID:%d \ n", Getpid ());

printf ("My parent ' s PID:%d \ n", Getppid ());//output current process pid and parent process PID

return 0;

}

Testsystem.c

#include <stdlib.h>

int main (int argc, char *argv[])

{

System (argv[1]);//execute Shell commands and send instructions to Dos .

System (argv[2]);

return exit_success;

}/*----------end of function main----------*/

waitedemo1.c

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

#define DELAY 4

void Child_code (int delay)

{

printf ("Child%d here. Would sleep for%d seconds\n ", getpid (), delay);

Sleep (delay);

printf ("Child-done. About to exit\n ");

Exit (17);

}

void Parent_code (int childpid)

{

int wait_rv=0; /* return value from Wait () */

WAIT_RV = Wait (NULL);

printf ("Done waiting for%d. Wait returned:%d\n",

Childpid, WAIT_RV);

}

int main ()

{

int newpid;

printf ("Before:mypid is%d\n", getpid ());

if ((Newpid = fork ()) = = =-1)

Perror ("fork");

else if (Newpid = = 0)

Child_code (DELAY);

Else

Parent_code (NEWPID);

If there are child processes, the child process is terminated and the child process PID is successfully returned

return 0;

}

#include <stdio.h>

#include <stdlib.h>

Waitdemo2.c

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

#define DELAY 10

void Child_code (int delay)

{

printf ("Child%d here. Would sleep for%d seconds\n ", getpid (), delay);

Sleep (delay);

printf ("Child-done. About to exit\n ");

Exit (27);

}

void Parent_code (int childpid)

{

int WAIT_RV;

int child_status;

int High_8, low_7, bit_7;

WAIT_RV = Wait (&child_status);

printf ("Do waiting for%d. Wait returned:%d\n", Childpid, WAIT_RV);

High_8 = Child_status >> 8; /* 1111 1111 0000 0000 */

low_7 = Child_status & 0x7F; /* 0000 0000 0111 1111 */

bit_7 = Child_status & 0x80; /* 0000 0000 1000 0000 */

printf ("status:exit=%d, sig=%d, core=%d\n", High_8, Low_7, bit_7);

}

int main ()

{

int newpid;

printf ("Before:mypid is%d\n", getpid ());

if ((Newpid = fork ()) = = =-1)

Perror ("fork");

else if (Newpid = = 0)

Child_code (DELAY);

Else

Parent_code (NEWPID);

}//This is more than 1 of the state of a sub-process, split the state into three,exit,sig and core

Reference: Kang Jiaxin Student's Blog

Experience: Study the role of the students explain function, and run the code, understand the running process, learning.

11-Week Study report

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.