How can I make the system call write() print to the screen?

前端 未结 5 1531
故里飘歌
故里飘歌 2021-02-01 22:48

For my OS class I\'m supposed to implement Linux\'s cat using only system calls (no printf)

Reading this reference I found it being used to print to a file. I guess I sh

5条回答
  •  你的背包
    2021-02-01 23:23

    #define _GNU_SOURCE         /* See feature_test_macros(7) */    
    #include  // For open, close, read, write, fsync
    #include   //For SYSCALL id __NR_xxx
    
    //Method 1 : API    
    write(1,"Writing via API\n",\
            strlen("Writing via API\n") ); 
    fsync(1);
    //Method 2  : Via syscall id
    const char msg[] = "Hello World! via Syscall\n";
    syscall(__NR_write, STDOUT_FILENO, msg, sizeof(msg)-1);     
    syscall(__NR_fsync, STDOUT_FILENO );    // fsync(STDOUT_FILENO);
    

提交回复
热议问题