小编Gol*_*olu的帖子

在c语言中使用fork

现在我在理解fork()系统调用的工作方面遇到了问题.我写了一个代码如下:

#include<stdio.h>
int main()
{
    int a, b;
    b=fork();

    printf("\n the value of b = %d",b);
}
Run Code Online (Sandbox Code Playgroud)

此代码的输出如下:

现在我不明白为什么输出是这样的?

之后,我只是在我的代码中添加一行,输出完全不同.我的代码如下:

int main()
{
    int a, b;
    b=fork();
Run Code Online (Sandbox Code Playgroud)

当我运行代码输出跟随2389我的名字是manish

 the value of b = 0
Run Code Online (Sandbox Code Playgroud)

现在我对fork()通话的工作感到很困惑.

我的问题如下:

  1. 怎么样fork()
  2. 控制在fork()通话结束后的位置?
  3. 任何机构都可以解释为什么编写的代码输出有问题吗?
  4. 为什么b在不同位置发生的输出意味着在第一个代码中输出b = 2260就在输出之前,b = 0而值b = 2389不在...之前b = 0

请解释我在问题中编写的代码中的fork的工作,以便我可以正确地学习它.

c fork systems-programming

11
推荐指数
2
解决办法
4万
查看次数

c ++成员函数指针问题

我是c ++的新手.我想知道对象指针和指向成员函数的指针.我写了一个代码如下:

代码:

#include <iostream>
using namespace std;
class golu
{
   int i;
public:
   void man()
   {
      cout<<"\ntry to learn \n";
   }
};
int main()
{
   golu m, *n;
   void golu:: *t =&golu::man(); //making pointer to member function

   n=&m;//confused is it object pointer
   n->*t();
}
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时,它显示了两个错误,其中包括:

pcc.cpp: In function ‘int main()’:
pcc.cpp:15: error: cannot declare pointer to ‘void’ member
pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object
pcc.cpp:18: error: ‘t’ cannot be used as a function.
Run Code Online (Sandbox Code Playgroud)

我的问题如下:

  1. 我在这段代码中做错了什么? …

c++ member-function-pointers

9
推荐指数
2
解决办法
4365
查看次数

C语言和多线程编程

我的问题与C中的线程编程有关.

我的问题是我只想在main程序中创建两个线程.这两个线程应该按顺序工作,这意味着我的第一个线程应该首先执行(不应该执行任何线程的其他语句).第一个线程应该完全控制.main在第一个线程完成之前,不应执行任何其他线程的任何其他语句,甚至程序语句.

完成第一个线程后,第二个线程应以与第一个线程类似的方式执行.

之后我的主要应该执行.

我知道你可以说我为什么要这样做,因为这个东西可以通过创建两个函数并按顺序调用它们来实现,但是对于学习和实验我想在线程的帮助下完成它.

我用C编写了一些代码如下:

void* fun()
{  
    printf("\nThe thread 1 is running");
}
void* van()
{
    printf("\nthread 2 is running ");
}

int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,fun,NULL);
    pthread_create(&t2,NULL,van,NULL);
    printf("\nI'm in main\n");
    pthread_join(t2,NULL); 
}
Run Code Online (Sandbox Code Playgroud)

该程序工作正常,但我不理解该功能的工作pthread_join().

当我更改我的代码时,如下所示:

int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,fun,NULL);
    pthread_join(t2,NULL);  // Change
    pthread_create(&t2,NULL,van,NULL);
    printf("\nI'm in main\n");
}
Run Code Online (Sandbox Code Playgroud)

现在,当我运行代码时,它显示了一个分段错误.

现在我的问题如下:

  1. pthread_create()函数中的属性参数是什么?我们为什么要使用它们?线程的默认属性是什么?请举例说明.
  2. 功能上的论点是pthread_create()什么?为什么我们使用它们?线程的默认参数是什么?请举例说明.
  3. pthread_join()实际上是如何工作的?当我的代码pthread_join()在main中t2作为第一个参数调用时,它意味着什么.这是否意味着主要应该暂停执行直到t2执行完成或其他什么?
  4. 第二个论点是pthread_join()什么?我们为什么用它?它的默认值是多少?请用示例或代码解释.

c multithreading pthreads

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

c递归程序问题

我是递归概念的新手.我想编写一个递归函数,它将float和integer作为参数,并以浮点值保持不变且整数值更改的方式递归调用它

我写下面的代码:

#include <stdio.h>

float sum(float f, int k)
{
    static float c;
    c = f - k;
    c = sum(f, k - 1);
    return c;
}

int main()
{
    float f, g = 10.00;
    int i = 5;
    f = sum(g, i);
    printf("the sum of integer and float = %f", f);
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时它没有显示错误,但是当我运行程序时它显示了一个分段错误.

我的问题如下:

  1. 代码有什么问题?
  2. 为什么它显示分段错误?
  3. 如何在具有多个参数的函数中使用递归?

请解释一下递归函数的一些例子,它有两个参数.

c c++ segmentation-fault

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