小编bvk*_*256的帖子

如何让程序重启?(Linux进程)

我正试图让我的程序重新启动,但似乎没有任何工作.我尝试过使用fork(),但在杀死这个 parent过程后,它也 child被杀死了.

void sigup_handler(int signum) {
    int pid = fork();
    if (pid == 0) {
        execve("prog2", NULL); 
    }
    else
        kill(getpid(), SIGTERM);
}

int main() {
    puts("Program 2 started.");
    signal(SIGHUP, sigup_handler);
    sleep(50); 
    puts("Program 2 terminated.");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c linux operating-system process

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

奇怪的并发代码行为

我目前正在学习Java并发.我对代码行为的方式感到非常惊讶.

import java.util.concurrent.*;

public class Exercise {
    static int counter = 0;

    static synchronized int getAndIncrement() {
        return counter++;
    }

    static class Improper implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 300; i++) {
                getAndIncrement();
            }
        }
    }


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 300; i++) {
            executorService.submit(new Improper());
        }
        executorService.shutdown();
        System.out.println(counter);
    }
}
Run Code Online (Sandbox Code Playgroud)

它不应该一直输出90000吗?相反,结果总是不同的.

java multithreading

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

代理InputStream的方法

我正在使用Android-Universal-Image-LoaderHTTPS在我的Android应用程序上从远程服务器加载图像.要访问映像,客户端应提供有效的令牌,有时服务器可能会返回"过期的crsf令牌"错误.为了处理此行为,应定义自定义ImageDownloader.下面是我的实现中应该覆盖的方法的基本实现.

protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
    HttpURLConnection conn = createConnection(imageUri, extra);

    int redirectCount = 0;
    while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
         conn = createConnection(conn.getHeaderField("Location"), extra);
         redirectCount++;
    }

    InputStream imageStream;
    try {
         imageStream = conn.getInputStream();
    } catch (IOException e) {
         // Read all data to allow reuse connection (http://bit.ly/1ad35PY)
         IoUtils.readAndCloseStream(conn.getErrorStream());
         throw e;
    }
    if (!shouldBeProcessed(conn)) {
         IoUtils.closeSilently(imageStream);
         throw new IOException("Image request failed with response code " + conn.getResponseCode());
    } …
Run Code Online (Sandbox Code Playgroud)

java android java-io

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

Clojure中标准输出的同步

我有一个用Clojure编写的多线程应用程序.当多个线程写入STDOUT时,存在在控制台显示中正确显示文本的问题.我怎么能在Clojure中正确地做到这一点,所以线条看起来不会隔行扫描?我认为这将涉及某种单独的IO代理,但我不确定如何做到这一点.

concurrency clojure

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

无法禁用android.widget.Button

我在Android小部件中有一个按钮,声明如下:

<Button android:id="@+id/newWorkBtnWidget"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:text="@string/ts_on_repair"/> 
Run Code Online (Sandbox Code Playgroud)

每当我尝试通过RemoteViews启用或禁用此按钮时,我都会收到错误消息 android.widget.RemoteViews$ActionException: view: android.widget.Button can't use method with RemoteViews: setEnabled(boolean)

我的代码:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                        R.layout.widget);

remoteViews.setInt(R.id.newWorkBtnWidget, "setBackgroundResource",
    R.drawable.green_button);
remoteViews.setBoolean(R.id.newWorkBtnWidget, 
   "setEnabled", false);
Run Code Online (Sandbox Code Playgroud)

怎么解决?

android

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

当软键盘上升时,如何使按钮保持在底部

我在屏幕底部有一个按钮,即使软键盘上升,我也希望它能保持稳定,但目前它还是软键盘.我试图将按钮对齐到底部,但没有成功.

这是代码(按钮在id/activity_form_button_frame中):

<RelativeLayout>
     <RelativeLayout
        android:id="@+id/activity_form_button_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
    <ImageButton
        android:id="@+id/activity_form_next_button"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/black"
        android:src="@drawable/next_btn_drawable"
        android:scaleType="fitCenter"
        android:padding="5dp"
        />
    <Button
        android:id="@+id/activity_form_sumbit_button"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/black"
        android:text="SUBMIT"
        android:textColor="@color/buttonBlue"
        android:visibility="gone"
        android:padding="15dp"
        style="@android:style/TextAppearance.Large"/>
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/activity_form_fragmentcontainer"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/activity_form_button_frame"/>

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout

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

找出所有可能的欧拉循环

我已经实现了一种算法,用于在无向图中查找给定起始顶点的欧拉循环(使用 DFS 并删除访问过的边),但它总是只返回一条路径。如何修改算法以搜索顶点的所有可能的欧拉循环?

这是相关代码:

typedef int Graph[200][200]; // adjacency matrix
int v, e; // vertex count, edge count

......

void DFS(Graph &G, int x) {
    int i;
    Push(x);
    for (i = 0; i < v; i++)
        if (G[i][x] > 0) {
            G[i][x] = 0;
            G[x][i] = 0;
            DFS(G, i);
            break;
    }
Run Code Online (Sandbox Code Playgroud)

}

algorithm graph-theory graph

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

如何更改按钮波纹效果的颜色

由于某种原因,我无法更改按钮的波纹效果的颜色。我在这里做错了什么?

androidx.compose.material.Button(
    onClick = onClick,
    modifier = modifier
        .indication(
            interactionSource = interactionSource,
            indication = rememberRipple(color = Color.Red)
            )
        .navigationBarsPadding(),
    enabled = enabled,
    interactionSource = interactionSource,
    elevation = elevation,
    shape = RectangleShape,
    colors = ButtonDefaults.bottomColors(),
    contentPadding = PaddingValues(vertical = 4.dp),
    content = { Text(text) },
)
Run Code Online (Sandbox Code Playgroud)

android android-jetpack-compose android-jetpack-compose-button

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

在nasm中寻址数组元素

我是装配和NASM的新手,还有一个代码:

    SECTION .data       
array db 89, 10, 67, 1, 4, 27, 12, 34, 86, 3
wordvar dw      123     

    SECTION .text       
        global main     
main:               

    mov eax, [wordvar]
    mov ebx, [array+1]
    mov ebx,0       
    mov eax,1       
    int 0x80    
Run Code Online (Sandbox Code Playgroud)

然后我通过GDB运行可执行文件eax寄存器按预期设置为值123,但在ebx中有一些混乱而不是数组元素值.

assembly nasm

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

如何在fork()之后将命令行参数传递给子进程

我有以下代码草案.

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>    

int main(int argc, char *argv[])
{

    printf( "usage: %i filename", argc );

    pid_t pID = fork();
    if (pID == 0)                // child
    {
        // Code only executed by child process
        printf("Child PID: %i", pID);

        int file = open("/tmp/rtail", O_CREAT | O_WRONLY);

        //Now we redirect standard output to the file using dup2
        dup2(file,1);

        char tmp[30];
        sprintf(tmp, "cat `tail -f %s`", argv[1]);
    }
    else if (pID < 0)            // failed to fork
    { …
Run Code Online (Sandbox Code Playgroud)

c linux operating-system system-calls

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