AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-3047729

Addem's questions

Martin Hope
Addem
Asked: 2024-12-07 00:28:40 +0800 CST

四开本内联碎片不会破碎成盒子

  • 6

在 Quarto 演示文稿中,我尝试更改单行内部分文本的颜色,并使片段一个接一个地出现。这是我目前的 QMD 文件:

---
title: "Mathematical Reasoning"
format: revealjs
incremental: true
---

:::: {.fragment}

Consider the following paradoxes.  

::: {.inline-fragment}
<span style="color:gray">(or just curiosities)</span>
:::

::::

以下内容添加到reveal.css

.inline-fragment {
  display: flex;
}

受到这个相关问题的答案的启发: 使用 quarto 和 revealjs 如何让一个单词出现在幻灯片的一行上?

(该reveal.css文件是 Quarto 在首次呈现演示文稿时自动生成的。完整文件太长,由于字符限制,我无法将其包含在此处 - 这是一个非常大的文件。但由于这个原因,我无法提供我所描述的完整工作示例,所以我只描述了上述 CSS 已添加到这个自动生成的文件中。)

但是,在我的用例中,这会导致第一个片段中的内容换行并形成一个文本框。然后第二个片段不会以内联方式呈现,而是在第一个框旁边的自己的框中呈现。

如果有人知道如何使这些片段呈现为就像写在一行中一样,我将不胜感激。

css
  • 1 个回答
  • 16 Views
Martin Hope
Addem
Asked: 2024-08-06 02:06:14 +0800 CST

`shmget` 无效参数错误——内存是否仍从上次执行分配?

  • 6

我有以下程序来练习使用分叉和共享内存。

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> 
#include <unistd.h> 
#include <sys/wait.h>

#include <sys/shm.h>
#include <sys/ipc.h>

int main() {
    int bignum = 1000000;
    
    key_t key = ftok(".", 'x');
    int shmid = shmget(key, sizeof(int)*bignum, IPC_CREAT | 0666);
    if (shmid < 0) {
        perror("shmget\n");
        return 1;
    }
    int *arr = shmat(shmid, NULL, 0);
    pid_t c1 = fork();
    if (c1==0) {
        pid_t c2 = fork();
        if (c2==0) {
            pid_t c3 = fork();
            if (c3==0) {
                arr[0] = 10;
            } else {
                arr[1] = 11;
            }
            wait(NULL);
            exit(0);
        } else {
            arr[2] = 12;
        }
        wait(NULL);
        exit(0);
    } else {
        arr[3] = 13;
        wait(NULL);

        for (int i=0; i<4; i++) printf("%d ", arr[i]);
        printf("\n");
    }


    shmdt(arr);
    shmctl(shmid, IPC_RMID, NULL);
    exit(0);
}

我之前使用较少的共享内存运行此程序,现在增加了共享内存,以查看这是否会对程序产生任何影响。当我运行此程序时,我得到:

shmget
: Invalid argument

我看过这个帖子:C linux shmget 参数无效

我尝试过遵循建议,但我不熟悉ipcs和ipcrm。我在终端中运行ipcs,它给了我一些共享内存信息。但我无法分辨哪个是程序分配的,有很多,我无法分辨哪些可以安全删除,哪些不可以。

我觉得很奇怪,你不会在 C 程序内部这样做,所以这让我怀疑是否没有更好的方法来解决此问题。我特别不明白为什么调用shmdt和shmctl不会让这个问题不成问题。还有其他方法可以“撤消”内存共享吗?


编辑:该问题已被关闭,因为它与此类似:

C - System V - 删除共享内存段

但是,它使用了我的代码中已有的代码——因此它似乎无法回答这个问题。


编辑:这是我根据答案更新的代码。它确实解决了之前的问题,但现在经过一些错误检查后,引发了权限错误。

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> 
#include <unistd.h> 
#include <sys/wait.h>

#include <sys/shm.h>
#include <sys/ipc.h>

int main() {
    size_t bignum = 1000000*sizeof(int);
    
    key_t key = ftok(".", 'x');
    int shmid = shmget(key, bignum, IPC_CREAT | 0666);
    if (shmid == -1) {
        shmctl(shmid, IPC_RMID, NULL);
        shmid = shmget(key, bignum, IPC_CREAT | 0666);
        if (shmid == -1) {
            perror("shmget");
            exit(1);
        }
    }
    int *arr = shmat(shmid, NULL, 0);
    if (arr == (int *)-1) {
        perror("shmat");
        exit(1);
    }

    shmdt(arr);
    shmctl(shmid, IPC_RMID, NULL);

    shmid = shmget(key, bignum, IPC_CREAT | 0666);
    arr = shmat(shmid, NULL, 0);

    pid_t c1 = fork();
    if (c1==0) {
        pid_t c2 = fork();
        if (c2==0) {
            pid_t c3 = fork();
            if (c3==0) {
                arr[0] = 10;
            } else {
                arr[1] = 11;
            }
            wait(NULL);
            exit(0);
        } else {
            arr[2] = 12;
        }
        wait(NULL);
        exit(0);
    } else {
        arr[3] = 13;
        wait(NULL);

        for (int i=0; i<4; i++) printf("%d ", arr[i]);
    }

    if (shmdt(arr) == -1) {
        perror("shmdt");
        exit(1);
    }
    if (shmctl(shmid, IPC_RMID, NULL) == -1) {
        perror("shmctl");
        exit(1);
    }

    exit(0);
}

具体来说,当我运行它时,结果是

shmget: Permission denied
c
  • 1 个回答
  • 33 Views
Martin Hope
Addem
Asked: 2024-08-06 00:19:40 +0800 CST

为什么一个进程在分叉时没有达到“exit(0)”?

  • 7

我有以下程序,只是为了测试我是否理解了分叉和共享内存。总之,它创建共享内存、分叉、分叉、分叉,并在每个分叉中写入数据并退出。

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> 
#include <unistd.h> 
#include <sys/wait.h>

#include <sys/shm.h>
#include <sys/ipc.h>

int main() {
    
    key_t key = ftok(".", 'x');
    int shmid = shmget(key, sizeof(int)*4, IPC_CREAT | 0666);
    if (shmid < 0) {
        perror("shmget\n");
        return 1;
    }
    int *arr = shmat(shmid, NULL, 0);
    pid_t c1 = fork();
    if (c1==0) {
        pid_t c2 = fork();
        if (c2==0) {
            pid_t c3 = fork();
            if (c3==0) {
                arr[0] = 10;
            } else {
                arr[1] = 11;
            }
            exit(0);
        } else {
            arr[2] = 12;
        }
        exit(0);
    } else {
        arr[3] = 13;
        wait(NULL);
        wait(NULL);
        wait(NULL);

        for (int i=0; i<4; i++) printf("%d ", arr[i]);
        printf("\n");
    }
    exit(0);
}

这将打印0 11 12 13出由于某种原因该值10从未被分配给arr[0]。

我预计每个进程都会到达某个对 的调用exit(0)。尤其是第三次 fork 之后的进程,我认为应该会到达exit(0)与第二次 fork 相同的对 的调用。

但是,当我在第三个 fork 块内(即 after c3==0)添加对 exit 的明确调用时,它就会按预期执行。

当我写这篇文章时,我突然想到了一个关于为什么会发生这种情况的猜测:是不是因为第二个子进程在第三个子进程之前到达退出,并且因为它退出了,所以从第四个子进程到父进程的链接就断了?

c
  • 1 个回答
  • 61 Views
Martin Hope
Addem
Asked: 2024-07-27 02:54:14 +0800 CST

为什么打印会导致堆中断增加这么多?

  • 5

我写了下面的程序,

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "rand.h"

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

  void *x = sbrk(0);
  printf("The initial top of the heap is %p.\n", x);
  void *y = sbrk(0);
  printf("The current top of the heap is %p.\n", y);
  printf("The difference is %d (%x)\n", (int) (y-x), (int) (y-x));
  return 0;
}

我明白为什么堆中断不同,因为它必须为要打印的调用分配堆空间。

我不明白为什么在我的 x86_64 Linux 计算机上,差异具体是 25600 字节。

比如,字符串本身可能只需要少量的字节(每个字节一个字符,在堆中向字符串添加一些头数据),根本不需要 1000 个字节,对吗?

我有点猜测这可能与分页有关——目前我还不太明白这一点。但从简单的搜索来看,分页似乎每次只分配大约 4000 个字节,所以可能也不是这样,对吧?

也许各种includes 与此有关?老实说,我根本不知道这对堆内存有何影响。


不管怎样,直接的问题是:为什么该程序会导致堆内存移动25600字节?

c
  • 1 个回答
  • 48 Views
Martin Hope
Addem
Asked: 2024-03-15 03:51:37 +0800 CST

应用函子时是什么导致这种类型不匹配?

  • 5

我有以下文件:

SetMaker.mli

module type Element = sig
  type t
  val create : 'a -> t
  val compare : t -> t -> int
  val to_string : t -> string
end

module type Set = sig
  type t
  val empty : unit -> t
end

module Make (M : Element) : Set with type t = (M.t list)

SetMaker.ml

module type Element = sig
  type t
  val create : 'a -> t
  val compare : t -> t -> int
  val to_string : t -> string
end

module type Set = sig
  type t
  val empty : unit -> t
end

module Make (M:Element) = struct
  type t = M.t list
  let empty () = []
end

主要.ml

open Mylib

module IntEl : SetMaker.Element with type t = int = struct 
  type t = int
  let create (x:int) : t = x
  let compare x y = 
    if x < y then -1
    else if x = y then 0 
    else 1
  let to_string = string_of_int 
end
;;

我认为其中一些可能比它必须的更详细,但我只是尝试抛出额外的声明只是为了看看它是否不能解决问题或者可能在错误消息中提供更多信息。事实上,在一个相关的问题(OCaml Type Mismatch Error with Functor-Based Dictionary Insertion)中,放置额外的类型声明解决了这个问题,但在这里似乎没有解决它。

当我尝试编译时出现错误

Error: Signature mismatch:
       ...
       Values do not match:
         val create : t -> t
       is not included in
         val create : 'a -> t
       The type t -> t is not compatible with the type 'a -> t
       Type t is not compatible with type 'a 
       File "lib/SetMaker.mli", line 3, characters 2-22: Expected declaration
       File "bin/main.ml", line 5, characters 6-12: Actual declaration

我对此感到很困惑,因为我认为'a应该是一个类型变量,它与一致使用的任何类型兼容。所以我认为无论是什么类型都'a可能是类型。t

types
  • 1 个回答
  • 19 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve