小编Ete*_*ner的帖子

linux,write()系统调用在尝试写入文件时返回-1

在下面的程序中,write()在写入文件时返回-1.

   #include<sys/types.h>
   #include<sys/stat.h> 
   #include<fcntl.h>   
   #include<stdio.h>   
   #include<unistd.h> 
   #include<stdlib.h>
   int main() {

   int fd_r=0,fd_w=0;
   int w_ret=100;
   fd_r = open("reader.txt", O_RDONLY);

   fd_w = open("writer.txt",O_CREAT,S_IRWXU);

   char *buf = (char *)malloc(50);

   while(read(fd_r,buf,30))
   {

          w_ret =   write(fd_w,buf,30);
          printf("%d", w_ret);
   }
}
Run Code Online (Sandbox Code Playgroud)

问题:我无法调试为什么会发生这种情况.关于如何调试此类问题的代码和建议的更正非常受欢迎

c linux

1
推荐指数
1
解决办法
5449
查看次数

Unix表示怀疑 - 关于执行下面的程序

我有以下程序

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

int x = 1;

void ouch(int sig) {
    printf("OUCH!  dividing by zero!\n");
    x = 0; 
}

void fpe(int sig) {
    printf("FPE!  I got a signal: %d\n",sig);
    psignal(sig, "psignal");
    x = 1; 
}

int main(void) {
    (void) signal(SIGINT, ouch);
    (void) signal(SIGFPE, fpe);

    while(1)
    {
        printf("Hello World: %d\n",1/x);
        sleep(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:执行此程序时 - 当我从终端向程序提供SIGINT时 - ""OUCH!除以零!"输出 - 按预期.下一条消息是"FPE!我得到一个信号:8 psignal:浮点异常".这个消息继续 - 不停止.我的疑问是在调用fpe信号处理程序后,我将x设置为1.我因此期望Hello World应该是显示在输出中.

以下是我得到的输出的记录:

Hello World: 1
Hello World: 1
^COUCH!  dividing by zero!
FPE!  I …
Run Code Online (Sandbox Code Playgroud)

unix linux signals linux-kernel

1
推荐指数
1
解决办法
1008
查看次数

我们可以编写一个在C++中不使用预处理器的可移植包含保护程序吗?

我们可以编写一个在C++中不使用预处理器的可移植包含保护程序吗?如果是这样,怎么办呢?

c++ c-preprocessor

0
推荐指数
1
解决办法
193
查看次数

c ++ 0x中的期货问题

我写了一个小程序,以了解c ++ 0x中的期货是如何工作的.在运行代码时,我得到一个错误,如"错误:'printEn'未在此范围内声明".我无法理解问题是什么.请指出我在这里做错了什么,如果可能的话,写相同的正确代码..

#include <future>
#include <iostream>

using namespace std;


int printFn()
{
    for(int i = 0; i < 100; i++) 
    {

        cout << "thread " <<  i << endl;
    }

    return 1;
}



int main()
{

    future<int> the_answer2=async(printEn);
    future<int> the_answer1=async(printEn);


   return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

在运行程序时对printFn进行更改后,我收到错误消息"找不到版本`GLIBCXX_3.4.14'(./a.out要求)".这表明了什么?

future c++11

0
推荐指数
1
解决办法
668
查看次数

来自mtjoseph的awk extract"Matthew":6/MKTCzMS/YU.:10974:10060:Matthew

如何提取" 马修从" ":6/MKTCzMS/YU:mtjoseph.10974:10060:马修 "

linux awk

0
推荐指数
1
解决办法
72
查看次数

linuux - execvp:执行ls命令 - 获取错误"ls:无法访问/ etc:没有这样的文件或目录"

下面的函数接受一个char指针数组Eg:arr [0]:ls arr [1]: - l arr [2]: - a arr [3]:/ etc arr [4]:NULL/*因为execvp需要最后是一个NULL*/

// function call is runCmd(arr); 函数定义如下:

void runCmd(char *arr[]){

        pid_t child_pid,tpid;
        int child_status;

        child_pid = fork();

        if(child_pid == 0){

                /* The child process executes the exec*/

                execvp(arr[0],arr);

                /*if it returns it must have failed */
                fflush(stdout);
                printf("Unknown Command \n");
                exit(0);
        }
                else {
                /*  let the parent wait for the child */
                do{
                         tpid = wait(&child_status);
                }while(tpid != child_pid);
        }
}
Run Code Online (Sandbox Code Playgroud)

执行后我收到消息 -

ls: cannot …
Run Code Online (Sandbox Code Playgroud)

linux fork exec

0
推荐指数
1
解决办法
3434
查看次数

使用SOCK_DGRAM从服务器获取信息时出现问题

我有以下程序(DGRAM套接字)来从时间服务器检索时间.该程序不会按预期打印出当前时间.

#define DAYTIME_SERVER_PORT  13

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

    int connectionFd, in;
    struct sockaddr_in servaddr;
    char buf[BUFSIZ];

    connectionFd = socket(AF_INET, SOCK_DGRAM, 0);
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(DAYTIME_SERVER_PORT);
    inet_pton(AF_INET,"time.mit.edu", &servaddr.sin_addr);

    sendto( connectionFd,buf ,1, 0,(struct sockaddr_in *)&servaddr, sizeof(servaddr) );

    in = recv(connectionFd, buf, BUFSIZ, 0 ) ; 
    buf[in] = 0 ; 
    printf("Time is %s \n",buf);

    close(connectionFd);


}
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 我在哪里弄错了?任何代码更改和宝贵的建议表示赞赏.我不知道如何使用recvform函数,因此尝试了recv.我连接的日间服务器是time.mit.edu

c sockets linux

0
推荐指数
1
解决办法
599
查看次数

标签 统计

linux ×5

c ×2

awk ×1

c++ ×1

c++11 ×1

c-preprocessor ×1

exec ×1

fork ×1

future ×1

linux-kernel ×1

signals ×1

sockets ×1

unix ×1