小编abc*_*abc的帖子

具有多个子创建的代码中的问题

这段代码是我的操作系统项目的一部分,项目要求做一些应用并发进程的东西,我决定用两个玩家做一个客户服务器扑克项目,我用儿子和孙子进程来确定手牌价值.

代码中应用的方案如下:

1 http://f.cl.ly/items/2f3x3z1A3z1n3r2b0X0N/schema.jpg

代码的问题在于,在同一游戏中只有第一只手被正确评估,实际上第二只手是不正确的,而在第三局游戏中有错误并且程序结束,这发生在每一个新游戏中

这是代码:

void check_hand(int suits[5],int ranks[5],int *point){

    pid_t son[2];
    int i,j;

    for (i = 0; i < 2; i++){
            son[i]=fork();

    /***********************************************
            straight flush son
    ************************************************/

            if(son[i]==0 && i==0){
                    pid_t grandson[3];
                    int k;
                    for(k=0;k<3;k++){
                            grandson[k]=fork();
                            if(grandson[k]==0 && k==0){
                                    exit(F_highcard(ranks));
                            }
                            else if(grandson[k]==0 && k==1){
                                    exit(F_flush(suits));
                            }
                            else if(grandson[k]==0 && k==2){
                                    exit(F_straight(ranks));
                            }
                            else if(grandson[k]<0){
                                    puts("fork failed");
                                    exit(-1);
                            }
                    }
                    int exit_status_straight,exit_status_flush,exit_status_highcard;
                    //waiting his sons
                    waitpid(grandson[0],&exit_status_highcard,0);
                    waitpid(grandson[1],&exit_status_flush,0);
                    waitpid(grandson[2],&exit_status_straight,0);

                    /**checkpoint A****/

                    //elaborate the exit statuses and exit with a …
Run Code Online (Sandbox Code Playgroud)

c fork multiple-processes

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

具有不同语义的函数调用

考虑这个代码有3个不同的函数调用语义:

void f(void){
   puts("OK");
}

int main(void){
   f();
  (*f)();
  (&f)();

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

第一种是调用f的标准方式,

第二个是解除引用函数指针的语义,

但在第三个我将&运算符应用于函数名称,它似乎工作正常.

在第二和第三种情况下会发生什么?

谢谢.

c function

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

禁止从类外部使用默认构造函数

考虑以下数据类。我想阻止使用直接方法创建对象__init__

from __future__ import annotations
from dataclasses import dataclass, field

@dataclass
class C:
    a: int

    @classmethod
    def create_from_f1(cls, a: int) -> C:
        # do something
        return cls(a)
    @classmethod
    def create_from_f2(cls, a: int, b: int) -> C:
        # do something
        return cls(a+b)

    # more constructors follow


c0 = C.create_from_f1(1) # ok

c1 = C()  # should raise an exception
c2 = C(1) # should raise an exception
Run Code Online (Sandbox Code Playgroud)

例如,我想强制使用我定义的附加构造函数,如果直接将对象创建为c = C(..).

到目前为止我所尝试的如下。

@dataclass
class C:
    a : int …
Run Code Online (Sandbox Code Playgroud)

python class python-3.7 python-dataclasses

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

指针和整数C之间的比较和赋值

我对这两个陈述有一个理论上的问题:

假设p指向Integer和aInteger 的指针:

a)if(p==a){.....}if(p>a)..

b)p=a;

所有这些都是非法的,b特别危险,但标准C如何考虑它们?

阅读标准,我没有发现它们是错误,未定义的行为,未指定的行为,违反约束,如果其中一个是合法的或其他的.

看着无数类似的问题,我还没有找到解决方案.

c pointers

5
推荐指数
2
解决办法
775
查看次数

uml状态机图中的if条件

考虑到这种情况:

...下订单时,会检查可用性,如果通过,则准备工作将开始,否则订单将被拒绝。

如果客户决定在开始准备之前使用信用卡付款,则订单价格将锁定在信用卡上。

准备工作完成后,订单即会交付,如果下单后超过 30 分钟,则可享受 50% 的折扣。

我的疑问是如何在状态机图中对 if 条件进行建模,我会按以下方式对其进行建模,但我不确定这是正确的方法:

我应该如何对状态机图中的 if 条件进行建模?

uml state-machine

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

pandas - 分组加权条形图

考虑以下由 10 行组成的 DataFrame。

d = {
    'grp_id':[1,2,1,1,1,3,1,1,4,1],
    'weight':[1,2,1,1,1,3,1,1,4,4],
    'value': [1,2,1,3,2,1,4,1,1,3]
}
df = pd.DataFrame(d)
Run Code Online (Sandbox Code Playgroud)

加权直方图可以通过

df['value'].hist(histtype='bar', weights=df['weight'])
Run Code Online (Sandbox Code Playgroud)

由分组未加权条形图grp_id

df['value'].hist(by=df['grp_id'], histtype='bar')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想将两者结合起来并绘制一个按grp_id分组的加权条形图。 我尝试了以下两种方法但没有成功,因为我都得到了.
ValueError

df['value'].hist(by=df['grp_id'], weights=df['weight'], histtype='bar')
df['value'].hist(by=df['grp_id'], weights='weight', histtype='bar')
Run Code Online (Sandbox Code Playgroud)

ValueError:权重应该与 x 具有相同的形状

我正在使用的临时解决方案如下。

fig, axes = plt.subplots(2, 2)
for ax,(idx, grp) in zip(axes.flatten(), df.groupby('grp_id')):
    grp['value'].hist(weights=grp['weight'], histtype='bar', ax=ax)
Run Code Online (Sandbox Code Playgroud)

但是,我想问一下是否有直接的方法可以用熊猫来做到这一点。

python pandas

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

使用gcc编译器隐式int和隐式声明函数

我读了c99标准:

-remove implicit function declaration,

-remove implicit int.
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用-pedantic在c99模式下使用gcc编译器编译此代码时

main(void){
    f(3);
    return 0;
}


int f(int a){
    ....
}
Run Code Online (Sandbox Code Playgroud)

我期待2个错误,但我只收到2个警告:

-warning: return type defaults to ‘int’

-warning: implicit declaration of function ‘f’.
Run Code Online (Sandbox Code Playgroud)

它们不应该是c99中的错误吗?

http://gcc.gnu.org/c99status.html 在这两种情况下都写了"完成".

谢谢.

c c99

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

在ML中使用ref函数

考虑到ref运算符,我无法理解它的应用程序以及遵循指令的意义:

1.

在这个定义中,我定义了什么?

 - val ref x=ref 9;

 val x = 9 : int
Run Code Online (Sandbox Code Playgroud)

2.

在这里我用ref x:= ref 12做什么?

 - val x= ref 8;

 val x = ref 8 : int ref

 - ref x := ref 12; 

 val it = () : unit

 - x;

 val it = ref 8 : int ref
Run Code Online (Sandbox Code Playgroud)

ml sml

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

使用 Pulp 更新约束

假设有一个线性程序和以下形式的约束:

\n\n
4 x_1 + 3 x_2 \xe2\x89\xa4 10\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且您想要将其更新为

\n\n
4 x_1 + 3 x_2 + 10 x_3 \xe2\x89\xa4 10\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者

\n\n
3 x_2 \xe2\x89\xa4 10\n
Run Code Online (Sandbox Code Playgroud)\n\n

为了做到这一点,我从头开始“重写”约束,就像

\n\n
prob.constraints[0] = ...\n
Run Code Online (Sandbox Code Playgroud)\n\n

但对于很长的约束来说,这是非常低效的。

\n\n

是否有更简单的方法来添加或删除约束中的变量?

\n

python linear-programming pulp

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

在python中创建邻接矩阵

我想加载带符号(加权)图的 CSV 或文本文件并创建邻接矩阵。CSV 文件包含名为“FromNodeId”、“ToNodeId”和“Sign”的三列。我使用的代码如下:

G = nx.read_edgelist('soc-sign-epinions.txt', data = [('Sign', int)])
#print(G.edges(data = True))

A = nx.adjacency_matrix(G)
print(A.todense())
Run Code Online (Sandbox Code Playgroud)

我遇到了以下错误

ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than 
the maximum possible size
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?请建议我一种创建邻接矩阵的方法。

python matrix networkx

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