Fl2440--fork () function simple Application instance __ function

Source: Internet
Author: User

Today's poetry sharing:

On the days of the Partridge and the late day of the Southern Song of Xin Qiji

Late day, a sad, Willow pond Green is gentle. If the eyes are not left to hate, do not believe that the world has a bald head.

Intestine has broken, tears difficult to accept, Acacia Heavy on the small red mansion. Qingzhi has been blocked by mountains, the frequency of the bar does not free.

===============================================================================================================

Development platform: fl2440

===================================================================================================

To get to the point, the fork () function and the wait () function were mentioned in the player's application, and a simple application was written.

The fork () function creates a process that is almost exactly the same as the original process through system calls, where two processes can do exactly the same thing, but two processes can do different things if the initial parameters or incoming variables are different.

Once a process calls the fork () function, the system assigns resources to the new process, such as space for storing data and code. All the values of the original process are then copied to the new process, with only a few values different from the values of the original process. The equivalent of cloning a self.
One of the wonders of fork invocation is that it is only invoked once, but it can return two times, and it may have three different return values:
In the parent process, fork returns the process ID of the newly created child process, fork returns 0 in the subprocess, and if an error occurs, fork returns a negative value, and once the fork () function is used, we can start writing a simple application.

/********************************************************************************* * Copyright: (C) 2017 Qicheng *
 All rights reserved.         * * FILENAME:FORK.C * description:this file * * version:1.0.0 (05/04/2017) * 
 Author:yangni <497049229@qq.com> * changelog:1, Release initial version on "05/04/2017 03:04:56 PM" * ********************************************************************************/#include <stdio .h> #include <linux/input.h>//for input_event #include <sys/types.h>//for pid//for open () #includ E <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>//for fork () a nd write () and execl () #include <sys/wait.h>//for wait () #include <errno.h>//for perror () in
    T Main () {pid_t pid;
    int ret;
    int fd_beer;
    int fd_led[4];
    int i,k,j; Char Buf[50];
    
    struct Input_event event;
  
        if ((Fd_beer = open ("/dev/event0", O_RDWR)) < 0) {//Open buzzer device printf ("beep test");
   return 1; for (i=1;i<=4;i++) {snprintf (char *) buf,sizeof (BUF),/sys/class/leds/led%d/brightne
            SS ", I);
            Fd_led[i] = open (buf, O_RDWR);
                if (fd_led[i]<0) {printf ("can" T open the file led%d, i);
            return-1;

    }/* Create subprocess */pid = fork ();        
        if (PID > 0) {/* Buzzer part * * Event.type = ev_snd;
        Event.code = Snd_tone;    Event.value = 2000;
        
		Open BUZZER ret = write (Fd_beer, &event, sizeof (struct input_event));
        
		Sleep (2);      Event.value = 0;
		
		Turn off the buzzer ret = write (Fd_beer, &event, sizeof (struct input_event));    
 /* Play a song */if (pid==wait (NULL)) {execl ("/mp3/madplay", "Madplay", "/mp3/song/feihua.mp3", "null");       }/*led Part */else if (0 = = pid) {for (k=0;k<2;k++)//Loop two times {for (i   =1;i<=4;i++) {write (Fd_led[i], "1", 1);
            Cycle Light 4 LED lights sleep (1);    
            for (j=1;j<=4;j++) {write (fd_led[j], "0", 1);
	}} close (Fd_beer);	
	for (i=1;i<=4;i++) {close (fd_led[i]);
return 0;

 }
At first, only one process was running. When executing to PID = fork (), a child process is created immediately, and the child process gets the same resources as the parent process (which is equivalent to cloning), and then each does its own thing (at the same time). PID > 0, the parent process partially opens the buzzer, at this time the child process (pid==0) also circulates the LED lamp, two second Hofu process closes the buzzer. The parent process calls wait () at this time, waits for the child process to finish, and waits for the child process to complete the For loop, and executes the command (playing a song) within the wait function.



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.