gg

时间:2019-09-14
本文章向大家介绍gg,主要包括gg使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

编程题

3、12

#include <stdio.h>
#include <unistd.h>

int main()
{
    pid_t pid = fork();
    if(pid < 0){
        printf("Process creation failed");
    }
    else if(pid == 0){
        printf("Parent process pid:%d\n", getpid());
    }
    else{
        printf("Parenet proces, my process is id:%d child process's id: %d\n", getpid(), pid);
        sleep(1000);  //Ensure that the child process exits before the parent process
    }
    return 0;
}
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>

int main(int argc, char **argv)
{
    if (argc > 2){
        fprintf(stderr,"Args passed invaild!\n");
        return 1;
    }
    pid_t pid;
    pid = fork();
    if (pid < 0){
        fprintf(stderr,"Fork Failed\n");
        return 1;
    }
    else if (pid == 0){
        //child process
        int num = atoi(argv[1]);  //Convert the string to number
        printf("%d,",num);
        while (num != 1){
            if (num % 2 == 0){
                num = num / 2;
                if (num == 1) printf("%d\n",num);
                else printf("%d,",num);
            }
            else{
                num = 3*num + 1;
                printf("%d,",num);
            }
        }
    }
    else{
        //Parent process
        wait(NULL);  //Wait until the child process completed
        printf("Child Complete\n");
    }
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h> // read the number of the last error
#include <fcntl.h> // file descriptor CONSTANT mode values

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

    /*
     * File descriptor array.
     * When we create our pipe, we give pipe() this array and it will
     * put the reading[0] and writing[1] file descriptors for the pipe into it.
     */

    int pipeFds[2]; 
    int fileBytesLength;
    char buffer[100];
    char childBuffer[100];
    
    // Check if 3 arguments were supplied.
    if (argc != 3) {
      perror("Filecopy: filecopy.exe [target] [destination]. \n");
      exit(1);
    }
    
    char* srcFile = argv[1];
    char* dstFile = argv[2];

    // Attempt to create a pipe.
    if (pipe(pipeFds) < 0) {
      printf("Something went wrong creating the pipe! %s\n", strerror(errno));
      exit(1);
    }

    // Fork child process
    switch(fork()) {

      // If there was an errorforking a child process
      case -1:
        printf("Error forking child process. %s\n", strerror(errno));
        exit(1);
      
      // If the current executing process is a child process
      // Read the file from upstream parent process and write it to a new file.
      case 0: 
        close(pipeFds[1]);                                                        // Close writing end of pipe upstream.
        ssize_t num_bytes_child = read(pipeFds[0], childBuffer, sizeof(childBuffer));   // Read file contents from upstream pipe into childBuffer
        close(pipeFds[0]);                                                        // close reading upstream pipe when we're done with it

        int targetDesc = open(dstFile, O_CREAT | O_WRONLY);                                  // Open a file for writing, create file descriptor.
        write(targetDesc, childBuffer, num_bytes_child);                            // Write contents of buffer to new file descriptor.
        

      // If the current process is the parent process.
      // Read the file and send it down to the child process to write.
      default: 
        close(pipeFds[0]);                                              // close reading end of pipe downstream.
        int fileInDesc = open(srcFile, O_RDONLY);                       // Read file into file descriptor
        ssize_t num_bytes = read(fileInDesc, buffer, sizeof(buffer));   // Get number bytes to read
        write(pipeFds[1], buffer, num_bytes);                           // Write bytes to child process.
        close(pipeFds[1]);                                              // Close writing downstream pipe when we're done with it.

    }

    return 0;
}

原文地址:https://www.cnblogs.com/lihello/p/11520495.html