gcht101hello/code20240524.html 20240524
Sorce Code:MKPY103fork/fork.c
hello

    // MKCP103fork   20240524
    // description: basic fork, LEDs are blinking using FORK
    // board: Milk-V DUO 256MB 
    // MPU:RISC-V CPU 1 x C906@1GHz + 1 x C906@700MHz
    // SD card: DUOV1.1.0.img 
    // os  cat /etc/os-release NAME=Buildroot VERSION=20240228-1428
    // device: LEDs at GP18,GP19
    // language: C ,cross compile riscv64-unknown-linux-musl-gcc 
    // IDE: VScode using wsl ubuntu on windows11
    // duo-example/MKCP103fork
    //    | makefile
    //    | fork.c 
    //$ cd duo-examples
    //$ source envsetup.sh
    //$ cd MKCP103fork
    //$ make
    //--------------------------------------  
    //  target folder root/MKCP103fork
    //$ scp -O fork root@192.168.42.1:/root/MKCP103fork
    //$ ssh root@192.168.42.1
    //$ password: milkv
    //# cd MKCP103fork
    //# ./fork 
    //# ps -A
    //  2995 root     ./fork
    //  2996 root     ./fork
    //  2997 root     ./fork
    //  2998 root     ./fork
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    struct Codeinfo{
      char codeId[9];
      char function[9];
      long date;
    };
    struct Flasher{
        int gpio;
        int count; 
        int status;
    }; 
    struct Codeinfo ci={"MKCP103","fork",20240524};
    struct Flasher  fl={18,0};
    void gpioBegin(void); 
    int main() {
        printf("{\"codeId\":%s%s,\"date\":%ld}\n",ci.codeId,ci.function,ci.date); 
    //-----------------------------------------------
        int val =fork();     // generate child process 
        int val1=fork();
        printf("{\"forkReturnValue\":%d,\"PID\":%d}\n",val,getpid()); //val:0 child
        if (val==0&&val1==0) fl.gpio=18;  //
        if (val==0&&val1!=0) fl.gpio=19;  //
        if (val!=0&&val1==0) fl.gpio=20; // 
        if (val!=0&&val1!=0) fl.gpio=21;  //
        gpioBegin();
    //-----------------------------------------------
        while(1) {
            fl.count++;               // toggle status  
            fl.status=fl.count%2;
            printf("{\"PID\":%d,\"count\":%d,\"led%d\":%d}\n",getpid(),fl.count,fl.gpio,fl.status);
            digitalWrite(fl.gpio, fl.status);    // led toggle
            sleep(1);                            // interval 1sec 
        }
    }
    //-------------------------------------
    void gpioBegin(void){
        if(wiringXSetup("duo", NULL) == -1) {
            wiringXGC();
        }
        if(wiringXValidGPIO(fl.gpio) != 0) {
            printf("Invalid GPIO %d\n",fl.gpio);
        }
        pinMode(fl.gpio, PINMODE_OUTPUT);
    } 
    
    //-------------------------------
    /* result
    ./fork       
    ./fork       
    {"codeId":MKCP103fork,"date":20240524}
    {"forkReturnValue":2781,"PID":2780}
    {"PID":2780,"count":1,"led21":1}
    {"forkReturnValue":2781,"PID":2782}
    {"PID":2782,"count":1,"led20":1}
    {"forkReturnValue":0,"PID":2781}
    {"PID":2781,"count":1,"led19":1}
    {"forkReturnValue":0,"PID":2783}
    {"PID":2783,"count":1,"led18":1}
    {"PID":2780,"count":2,"led21":0}
    {"PID":2782,"count":2,"led20":0}
    {"PID":2781,"count":2,"led19":0}
    */
    
    /* milkv duo 256 pinout 
    GP0  1	40 VBUS
    GP1  2  39 VSYS(5V)
    GND  3  38 GND						
    GP2	 4  37 3V3_EN						
    GP3	 5  36 3V3(OUT)						
    GP4	 6  35 Boot-Switch						
    GP5	 7  34 Audio-Out						
    GND	 8  33 GND						
    GP6	 9  32 GP27	
    GP7	 10 31 GP26	
    GP8	 11 30 RUN						
    GP9	 12 29 GP22	
    GND	 13 28 GND						
    GP10 14 27 GP21
    GP11 15 26 GP20	
    GP12 16 25 GP19	LED 
    GP13 17 24 GP18	LED
    GND	 18 23 GND						
    GP14 19 22 GP17	
    GP15 20 21 GP16
    GP25 LED 	
    */