我这样读/proc/<pid>/status:
std::ifstream file(filename);
std::string line;
int numberOfLinesToRead = 4;
int linesRead = 0;
while (std::getline(file, line)) {
// do stuff
if (numberOfLinesToRead == ++linesRead) {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到在极少数情况下会std::getline挂起.
getline返回false./proc/<pid>/status?这个代码在Ubuntu 16.04上运行得很好,当我通过loopback接口抛出UDP字节时,打印正确的值(ETHERTYPE_IP):
#include <pcap.h>
#include <iostream>
#include <net/ethernet.h>
int main(int argc,char **argv)
{
char errbuf[PCAP_ERRBUF_SIZE];
auto pcap = pcap_open_live("lo0", BUFSIZ, 0, 1000, errbuf);
pcap_loop(pcap,0, [] (u_char *self, const struct pcap_pkthdr *header,
const u_char *packet) {
auto eth = (struct ether_header *) packet;
auto eth_type = ntohs(eth->ether_type);
std::cout << "eth_type: " << std::hex << eth_type << std::endl;
}, nullptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
netcat的:
? ~ nc -uv -l 54321
Listening on [0.0.0.0] (family 0, port 54321)
? ~ nc -4u localhost …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例:
class A():
def __init__(self):
self.veryImportantSession = 1
a = A()
a.veryImportantSession = None # ok
# 200 lines below
a.veryImportantSessssionnnn = 2 # I wanna exception here!! It is typo!
Run Code Online (Sandbox Code Playgroud)
如果我尝试设置未设置的成员,我怎么能这样做才会引发异常__init__?
上面的代码在执行时不会失败,但是给我一个有趣的时间来调试问题.
跟str一样:
>>> s = "lol"
>>> s.a = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'a'
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个无UI片段的业务逻辑,我必须测试.我尝试了两个选项,但都失败了.
1.使用AndroidTestCase并创建模拟活动.
以下代码
@Override
protected void setUp() {
Intent i = new Intent(getTestContext(), TestActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getTestContext().startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
抛出一个例外
Permission denied: checkComponentPermission() reqUid=10104
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.xxx.iabsample.test/.TestActivity } from ProcessRecord{40769510 28116:com.xxx.iabsample/10070 (pid=28116, uid=10070) requires null
Run Code Online (Sandbox Code Playgroud)
2.将ActivityInstrumentationTestCase2与模拟活动一起使用
码
public class IabTest extends ActivityInstrumentationTestCase2<TestActivity> {
public IabTest() {
super("com.xxx.iabsample.test", TestActivity.class);
}
}
Run Code Online (Sandbox Code Playgroud)
抛出一个例外
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.xxx.iabsample/.test.TestActivity }
Run Code Online (Sandbox Code Playgroud)
它似乎尝试从测试目标应用程序启动活动,而不是从测试应用程序启动.
那么,测试片段的正确方法是什么?
我看到在2.3.4和4.2之间onNewIntent调用的行为有所不同.
我有一个活动launchMode=singleTask.根据我对singleTask如何工作的理解,每次从任务列表恢复活动时,onNewIntent都应该调用.
这是2.3.4(LG P990)在我开始活动时发生的情况,按"home"将其移至前台然后从任务列表恢复(longpress"home"):
D/NewIntent(23314): onPause
D/NewIntent(23314): onNewIntent
D/NewIntent(23314): onResume
D/NewIntent(23314): onPause
D/NewIntent(23314): onNewIntent
D/NewIntent(23314): onResume
Run Code Online (Sandbox Code Playgroud)
4.2(Nexus 4)上相同:
D/NewIntent(12960): onPause
D/NewIntent(12960): onResume
D/NewIntent(12960): onPause
D/NewIntent(12960): onResume
Run Code Online (Sandbox Code Playgroud)
如您所见,未调用onNewIntent.
有人可以解释一下发生了什么吗?
这是一个相当简单的应用程序,可以通过clone()调用创建轻量级进程(线程).
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 1024*1024
int func(void* param) {
printf("I am func, pid %d\n", getpid());
return 0;
}
int main(int argc, char const *argv[]) {
printf("I am main, pid %d\n", getpid());
void* ptr = malloc(STACK_SIZE);
printf("I am calling clone\n");
int res = clone(func, ptr + STACK_SIZE, CLONE_VM, NULL);
// works fine with sleep() call
// sleep(1);
if (res == -1) {
printf("clone error: …Run Code Online (Sandbox Code Playgroud) 我读取了数据/proc/<pid>/environ并得到了奇怪的结果。这怎么可能?
open("/proc/24696/environ", O_RDONLY) = 10
read(10, "24694\nPPid:\t2606\nTracerPid:\t0\nUi"..., 4096) = 1470144576
open("/proc/25387/environ", O_RDONLY) = 10
read(10, "686\nPPid:\t1\nTracerPid:\t0\nUid:\t10"..., 4096) = 5905728
Run Code Online (Sandbox Code Playgroud)
为什么read返回的值比count(4096) 更大?
不过,这种情况并非每次都会发生。
Debian 7、3.2.0-4-amd64 #1 SMP Debian 3.2.84-1 x86_64 GNU/Linux
更新:
我不认为这是strace错误 - 我还打印了 glibcread函数的结果,结果相同。
更新2:
我创建了一个重现问题的测试应用程序,https://gist.github.com/lstipakov/70c5b5e96112c7f1f6204d70b2c8280e
它枚举文件下的所有进程/proc并从文件中读取environ。问题在不到一分钟内重现:
// do read, which sometimes returns weird values on 3.2.0-4-amd64 #1 SMP Debian 3.2.84-1 x86_64 GNU/Linux
char tmp[4096];
auto val = read(fd, tmp, sizeof(tmp));
if (val > …Run Code Online (Sandbox Code Playgroud) 我需要实现类似的东西:
if [ $i -ne $hosts_count - 1] ; then
cmd="$cmd;"
fi
Run Code Online (Sandbox Code Playgroud)
但我明白了
./installer.sh:124行:[:缺少`]'
我做错了什么?
这是我的脚本:
#/bin/bash
list="a b c"
for i in $list; do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
这有效:
? ~ ./lol.sh
a
b
c
Run Code Online (Sandbox Code Playgroud)
这不会:
? ~ . ./lol.sh
a b c
Run Code Online (Sandbox Code Playgroud)
为什么 split 不适用于 dot 命令,我该如何解决?
我有一个带有项目的微调器,通过ArrayAdapter填充.我想为spinner的组合框和列表框中的某些(不是所有)微调器项更改字体样式.我想我需要继承一些东西,但我不明白是什么.我怎样才能做到这一点?
谢谢!