Introduction to advanced programming in Unix environment: Basic Unix knowledge (III)

Source: Internet
Author: User

Introduction to advanced programming in Unix environment: Basic Unix knowledge (III)

Some shame ~, I haven't updated it for a year and a half. I came to a new organization and made an embedded system as I expected. I thought it was okay;
This time, I continue with the advanced programming series for the unix environment, hoping to stick to it :)

1. programs and processes
In fact, these two concepts are well explained: The program is the executable file stored on the disk; the process is the State where the executable file runs and then in the memory;
For example, the relationship between a program and a process is like a car. When a car is parked in a garage, it is a program. When it is running, it is a process;
2. process ID PID
A process is identified by a process number in unix and Unix-like systems. It is a non-negative integer;
You can write a simple program to print the PID value. The Code is as follows:

 
 
  1. #include
  2. #include
  3. #include

  4. int main(int argc, char * argv[])
  5. {
  6. pid_t pid = 0;
  7. pid = getpid();

  8. printf("this proccess id is %d\n", pid);
  9. return 0;
  10. }
The compiled statement is gcc-o pid. c.
The execution result is:
 
 
  1. ./pid
  2. this proccess id is 24047
  3. ./pid
  4. this proccess id is 24048
  5. ./pid
  6. this proccess id is 24050
  7. ./pid
  8. this proccess id is 24051
  9. # ./pid
  10. this proccess id is 24052
It seems that the PID is not the same each time. This ensures that the program has been started at each start. Generally, the pid is stored as one. pid file to check whether the program has been started;
Modified the program as follows:
 
 
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include

  7. int main(int argc, char * argv[])
  8. {
  9. pid_t pid = 0;
  10. char * pidfile = "./freesir.pid";
  11. pid = getpid();
  12. int fd = 0;
  13. char pids[16] = {0};

  14. if(access(pidfile, F_OK) != -1)
  15. {
  16. printf("this program is already run in system.\n");
  17. return -1;
  18. }

  19. fd = open(pidfile, O_TRUNC | O_CREAT | O_RDWR);
  20. if(fd < 0)
  21. {
  22. printf("open pid file error!\n");
  23. return -2;
  24. }

  25. sprintf(pids, "%d", pid);

  26. if(write(fd, pids, strlen(pids)) != strlen(pids))
  27. {
  28. printf("write pid file error!\n");
  29. return -3;
  30. }

  31. sleep(10);

  32. unlink(pidfile);
  33. close(fd);
  34. printf("this proccess id is %d\n", pid);
  35. return 0;
  36. }
After running the program, a freesir. pid file is created in the current directory. If you run the program multiple times, an error is reported. This is probably the case;

3. Process Control
Here is a brief introduction to process control. More details will be provided after advanced programming in the unix environment;
In the sample code, we only wrote a simple shell encapsulation. Let's explain it separately by writing two small programs;

 
 
  1. #include
  2. #include
  3. #include

  4. int main(int argc, char * argv[])
  5. {
  6. pid_t pid = 0;
  7. int status = 0;

  8. pid = fork();
  9. if(pid < 0)
  10. {
  11. printf("fork error!\n");
  12. return 0;
  13. }
  14. else if(pid == 0)
  15. {
  16. printf("i'm the child proccess. pid = %d\n", getpid());
  17. sleep(5);
  18. exit(127);
  19. }
  20. else if(pid > 0)
  21. {
  22. printf("i'm parent prccess. pid = %d, childpid = %d\n", getpid(), pid);
  23. }

  24. pid = waitpid(pid, &status, 0);
  25. if(pid < 0)
  26. {
  27. printf("waitpid error!\n");
  28. }
  29. return 0;
  30. }
The above program prints the pid of the Parent and Child processes. It can be seen that the child process returns 0, and the child process ID can be obtained using getpid; the parent process returns the child process PID;
Finally, waitpid is used to wait for the sub-process to exit. If an error occurs, it is simply printed. In fact, three macros of waitpid are used to determine the exit status of the sub-process;

 
 
  1. WIFEXITED(status)
  2. WEXITSTATUS(status)
  3. WIFSTOPPED(status)
These can be written later;

Let's take a look at it today. The more you see it, the more powerful linux is ....


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.