小编Mik*_*oll的帖子

信号号码?

如何快速从其编号中获取信号名称?有strsignal(),但我只想要名称,例如SIGUSR1

换句话说,如果我们有像SIGUSR1 -> 12 我们有类似的 宏 12 -> SIGUSR1吗?

c unix

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

将属性方法委托给父对象

我有以下课程:

class Alphabet

  attr_reader :letter_freqs, :statistic_letter

  def initialize(lang)
    @lang = lang
    case lang
    when :en
      @alphabet = ('A'..'Z').to_a
      @letter_freqs = { ... }
    when :ru
      @alphabet = ('?'..'?').to_a.insert(6, '?')
      @letter_freqs = { ... }
    ...
    end
    @statistic_letter = @letter_freqs.max_by { |k, v| v }[0]
  end

end

foo = Alphabet.new(:en)
Run Code Online (Sandbox Code Playgroud)

这里的中心成员是@alphabet.

我想使它成为一种容器类来直接调用Array方法

foo[i]
foo.include?
Run Code Online (Sandbox Code Playgroud)

而不是显式访问@alphabet:

foo.alphabet[i]
foo.alphabet.include?  
Run Code Online (Sandbox Code Playgroud)

我知道我可以定义很多方法

def [](i)
  @alphabet[i]
end
Run Code Online (Sandbox Code Playgroud)

但我正在寻找一种"继承"它们的正确方法.

ruby delegation

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

使用 setpgid() 对子进程进行分组

我只是不明白整件事。

我的进程树:

     0
    / \
   1   2
  /     \
 5       3
        / 
       4
Run Code Online (Sandbox Code Playgroud)

我想创建一个进程组 (3, 4, 5),并向该组发送来自 2 的信号。

我这样尝试过:

setpgid(pid3, pid3);
setpgid(pid4, pid3);
setpgid(pid5, pid3);

...

kill(-pid3, SIGUSR1);
Run Code Online (Sandbox Code Playgroud)

我应该在哪里放置我的setpgid()块?我尝试将其放入 3、0 和所有其他进程中,但setpgid()返回“没有这样的进程”或“不允许操作”。

pid 存储在文件中,因此我在调用之前检索它们setpgid()

c linux process

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

List <>中的"Tag"字符串

我有三种类型的字符串,我需要按特定的顺序放入一个List中."三种类型"我的意思是有三种处理字符串的方法.我想用struct这样的东西放在列表中:

struct Chunk
{
    public char Type;   // 'A', 'B' or 'C'.
    public string Text;
}
Run Code Online (Sandbox Code Playgroud)

也许有一种更好的方法来标记字符串应该如何处理?

c# string list

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

奇怪:printf()影响变量值

这是一段代码:

int somefunc() {
    /* ... */
    while ((pos = KMP_index(array, size, pattern, plen)) > -1) {
        count++;
    }
    return count;
}
Run Code Online (Sandbox Code Playgroud)

somefunc() 在多个子进程中调用,每个fork一次调用.

我的代码在Linux x86_64和i386上编译并按预期工作.但是当我在Atom上网本(Arch Linux i686)上运行它时,count变量永远不会超过2!

while (...) {
    count++;  //succesfully increments 
}
return count; //it's maximum 2!
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加printf():

while (...) {
    count++;  //succesfully increments
    printf("%d", anything);
}
return count; //value as expected
Run Code Online (Sandbox Code Playgroud)

打印空字符或fflushstdout在这里不起作用.我必须打印至少一个字符,然后才变量很好.它让我疯了.

有人可以告诉我,为什么我甚至不得不使用这种"解决方法"?这可能是我的linux环境的问题吗?(没什么特别的,GCC 4.8,库存内核)谢谢.

PS整个来源是http://pastebin.com/4eEHMbKn.是的,这是一个功课:)我需要创建一个类似grep的实用程序,在单独的进程中处理每个文件.

c linux

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

StringBuilder内存不足

我正在尝试使用以下字符串对我的解析器进行崩溃测试:

var theWholeUTF8 = new StringBuilder();
for (char code = Char.MinValue; code <= Char.MaxValue; code++)
{
        theWholeUTF8.Append(code);
}
Run Code Online (Sandbox Code Playgroud)

但是,测试在构建字符串时崩溃并抛出OutOfMemoryException.我错过了什么?

c# string

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

标签 统计

c ×3

c# ×2

linux ×2

string ×2

delegation ×1

list ×1

process ×1

ruby ×1

unix ×1