小编tij*_*jko的帖子

在PTRACE_CONT之后PTRACE_DETACH失败并且errno = ESRCH

在我的项目中,我需要附加到进程,恢复它们,然后再使用ptrace.但是,分离失败了errno=ESRCH (No such process).

如果我没有恢复进程,则分离工作正常PTRACE_CONT,但在这种情况下,进程停止/无响应,这在我的项目中是不可接受的.在Arch和Ubuntu 12.04 LTS上测试,结果相同.

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t pid = 21000;

    if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
        perror("PTRACE_ATTACH");
        return 1;
    } 
    printf("attached\n");
    waitpid(pid, NULL, WUNTRACED);

    if (ptrace(PTRACE_CONT, pid, NULL, NULL) == -1) {
        perror("PTRACE_CONT");
        return 1;
    }
    printf("continued\n");

    if (ptrace(PTRACE_DETACH, pid, NULL, NULL) == -1) {
        perror("PTRACE_DETACH");
        return 1;
    }
    printf("detached\n");

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

输出:

attached …
Run Code Online (Sandbox Code Playgroud)

c linux ptrace

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

Python嵌套的整数列表未按预期返回

我正在写这个函数,检查列表列表是否是一个有效的数独谜题.当我检查有效整数的列表时,我得到了意想不到的结果.

例如:

lst = [[1,2,3],[2,3,1],[4,2,1]]
for i in lst: 
  for v in i:
    print type(v)

<type 'int'>   #all the way through

for i in lst:
  for v in i:
    if v is int:
      print True
Run Code Online (Sandbox Code Playgroud)

什么都不打印,当我进入时:

for i in lst:
  for v in i:
    if v is not int:
      print False
Run Code Online (Sandbox Code Playgroud)

打印全部错误?不确定发生了什么,尤其是显示它们是整数的类型.

python types list

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

鼠标"点击"时的Pygame动作.rect?

我一直在编写一个测试函数来学习pygame.rect上的鼠标"click"动作将如何产生响应.

至今:

def test():
    pygame.init()
    screen = pygame.display.set_mode((770,430))
    pygame.mouse.set_visible(1)
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250,250,250))
    screen.blit(background, (0,0))
    pygame.display.flip()

    ## set-up screen in these lines above ##

    button = pygame.image.load('Pictures/cards/stand.png').convert_alpha()
    screen.blit(button,(300,200))
    pygame.display.flip()

    ## does button need to be 'pygame.sprite.Sprite for this? ##
    ## I use 'get_rect() ##
    button = button.get_rect()

    ## loop to check for mouse action and its position ##
    while True:
        for event in pygame.event.get():
            if event.type == pygame.mouse.get_pressed():
                ## if mouse is pressed get position of cursor …
Run Code Online (Sandbox Code Playgroud)

python pygame rect mouseevent

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

为什么打印列为内置?

在阅读dir(__builtins__)python2.7中列出的对象时,我注意到关键字 print在那里.

现在在python3.5下我可以看到它print,exec现在是对象.

这是什么原因?为什么甚至__builtins__开始列出它?特别是这个关键字,如果它从未在那里分组,它会有什么不同.似乎import也有可能被列入那里?

python python-2.7

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

valgrind抱怨在c做一个非常简单的strtok

嗨,我正在尝试通过将整个文件加载到char[]使用中来标记字符串fread.由于一些奇怪的原因,它并不总是有效,而valgrind在这个非常小的示例程序中抱怨.

给出类似的输入 test.txt

first
second
Run Code Online (Sandbox Code Playgroud)

以及以下方案

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>


//returns the filesize in bytes
size_t fsize(const char* fname){
  struct stat st ;
  stat(fname,&st);
  return st.st_size;
}

int main(int argc, char *argv[]){
  FILE *fp = NULL;
  if(NULL==(fp=fopen(argv[1],"r"))){
    fprintf(stderr,"\t-> Error reading file:%s\n",argv[1]);
    return 0;
  }
  char buffer[fsize(argv[1])];
  fread(buffer,sizeof(char),fsize(argv[1]),fp);
  char *str = strtok(buffer," \t\n");

  while(NULL!=str){
    fprintf(stderr,"token is:%s with strlen:%lu\n",str,strlen(str));
    str = strtok(NULL," \t\n");
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译像

gcc test.c -std=c99 -ggdb
Run Code Online (Sandbox Code Playgroud)

跑得像

./a.out …
Run Code Online (Sandbox Code Playgroud)

c c++ valgrind tokenize strtok

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

python检查列表中的字符串?

我试图遍历列表检查列表中的每个字符串的字符.

test = [str(i) for i in range(100)]

for i in test:
    if '0' or '4' or '6' or '8' in str(i):
        test.remove(i)
Run Code Online (Sandbox Code Playgroud)

我认为这样会好,但是列表是这样的:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
Run Code Online (Sandbox Code Playgroud)

删除'2'但是'41'不是?我注意到它只有偶数,但是,不知道为什么.

python list

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

将文件内容传送到 conky?

我一直在写一个脚本来检查天气。该脚本很好,并且作为 cronjob 工作得很好,但是,我现在试图将脚本(今天的天气预报)的结果显示在 conky 中。

我有一个 python-weather 脚本写入的文件“weather-outside”,然后我将它添加到 conkyrc 文件中:

${color grey}Weather:${scroll 16 $execi 3600 cat $/home/User/Desktop/misc./weather-outside | fold}
Run Code Online (Sandbox Code Playgroud)

execi命令是假设在时间间隔设置为3600secs运行,但,这后运行时Conky的是错误消息:

Conky: execi needs arguments
Conky: Error destroying thread
***** Imlib2 Developer Warning ***** :
    This program is calling the Imlib call:

    imlib_context_free();

    With the parameter:

    context

    being NULL. Please fix your program.
Run Code Online (Sandbox Code Playgroud)

我已将execi更改为exec但是,然后实际行 'cat $home/User/Desktop/misc./weather-outside | fold' 在 conky 上滚动而不是该文件中的文本?

linux conky

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

Python词典在解释器中重新排列?

可能重复:
Python字典,保持键/值与声明的顺序相同

当我注意到我在python解释器中创建的字典回来时出现了意想不到的结果时,我正在进行代码学院的一些练习.

我正在使用python2.7,当我输入:

>>> dict_a = {'x':9, 'y':10, 'z':20}
Run Code Online (Sandbox Code Playgroud)

我称之为:

>>> dict_a
{'y':10, 'x':9, 'z':20}
Run Code Online (Sandbox Code Playgroud)

所以我投入:

>>> dict_b = {'a':1, 'b':2, 'c':3}
>>> dict_b
{'a':1, 'c':3, 'b':2}
Run Code Online (Sandbox Code Playgroud)

我想我错过了一些基本的东西但是,经过一些搜索我不知道是什么?如果有人可以帮助解释为什么会发生这种情况,我们将非常感激.

python interpreter dictionary

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

Python返回范围列表的组合?

我正在根据范围列表返回列表的所有组合.

所以,如果我有:

test_list = [range(0,11,1),range(0,11,2),range(0,11,5)]
Run Code Online (Sandbox Code Playgroud)

我想根据范围返回包含所有可能组合的列表.例如:

output_list[[0,0,5],[0,0,10],[0,2,0],[0,4,0],[0,6,0].......]
Run Code Online (Sandbox Code Playgroud)

但我所能做的就是:

import itertools

test_list = [range(0,11,1),range(0,11,2),range(0,11,5)]
output_list = []
for i in itertools.permutations(test_list):
    if i not in output_list:
        output_list.append(i)
Run Code Online (Sandbox Code Playgroud)

哪个返回每个范围的置换,(范围列表再次)?

python list permutation

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

在 kubernetes pod 中运行 bash 脚本

我正在尝试使用以下文件运行外部bashyaml脚本。

该脚本位于该/scripts/run.sh文件夹内。我也已经给出了defaultMode: 0777

这是我得到的错误。

sh: 0: Can't open /scripts/run.sh
Run Code Online (Sandbox Code Playgroud)
apiVersion: v1
data:
  script.sh: |-
    echo "Hello world!"
    kubectl get pods
kind: ConfigMap
metadata:
  name: script-configmap
---
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app: script-job
  name: script-job
spec:
  backoffLimit: 2
  template:
    spec:
      containers:
        - command:
            - sh
            - /scripts/run.sh
          image: 'bitnami/kubectl:1.12'
          name: script
          volumeMounts:
            - name: script-configmap
              mountPath: /scripts
              subPath: run.sh              
              readOnly: false
      restartPolicy: Never
      volumes:
        - name: script-configmap
          configMap:
            name: script-configmap
            defaultMode: 0777
Run Code Online (Sandbox Code Playgroud)

kubernetes kubectl kubernetes-jobs

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

Python按字母数字字符串列表排序?

这是问题,我正在使用该sorted函数来排列字母数字字符串列表.所述字符串必须以字母分隔.

例如: sortqns(['s1q1', 's10q1', 's1q2', 's10q10', 's10q2'])

def cmpqn(a, b):
    if len(a) > len(b):
      return 1
    if len(a) < len(b):
      return -1
    if len(a) == len(b):
      return 0

def sortqns(qnlist):
    new = sorted(qnlist, cmp=cmpqn) 
    return new
Run Code Online (Sandbox Code Playgroud)

返回 ['s1q1', 's1q2', 's10q1', 's10q2', 's10q10']

我的问题是排序第二个数字:

sortqns(['s12q1', 's1q2', 's1q1'])

Returns ['s1q2', 's1q1', 's12q1']
Run Code Online (Sandbox Code Playgroud)

代替:

Returning ['s1q1', 's1q2', 's12q1']
Run Code Online (Sandbox Code Playgroud)

在第一个例子中,如果前两个项目也被交换,我想要的回报将被关闭.

python list sorted

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

Python为字符串中的字母返回整数?

我正在尝试将一串字母转换为整数,将'ABC'称为'123'.

python string ascii

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