N个进程和M种进程——进入和退出cs

发布于 2024-07-29 10:28:02 字数 468 浏览 5 评论 0原文

我被要求写:进入函数和退出函数对于以下情况:

有N个进程和M种进程(N>>M) 有一个临界区,所有相同类型的进程都可以进入该临界区。 例如:如果类型A在cs中,则类型B不能进入cs。 但所有类型为A的进程都可以进入。

我只能使用互斥锁和“类型”,即进程的类型。 不允许死锁。

你觉得这样可以吗?

shared: this.type = -1;
mutex m, m1=1;

enter{
    down(m)
    if (this.type == process.type) up(m1)
    down(m1)
    this.type= process.type
    up(m)
}
exit {
    this.type = -1
    up(m1)
}

谢谢! (顺便说一句,这不是硬件。我有一个考试,我正在解决前几年的测试)

I was asked to write: enter function and exit function for the following case:

there are N processes and M types of processes (N>>M)
there is a critical section in which all processes with the same type can enter.
for example: if type A is in cs, type B cannot enter cs. but all processes with type A can enter.

I can use only mutex and "type" which is the type of the process.
Deadlock is not allowed.

Do you think this is OK?

shared: this.type = -1;
mutex m, m1=1;

enter{
    down(m)
    if (this.type == process.type) up(m1)
    down(m1)
    this.type= process.type
    up(m)
}
exit {
    this.type = -1
    up(m1)
}

Thanks!
(by the way, this is not HW.I have an exam and I'm solving tests from previous years)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2024-08-05 10:28:02

我想了想你说的,确实我可以看到问题 - 10X!

添加一个计数器怎么样?
当同类型的第一个进程进入时 if counter==0 type==process.type
(初始化类型)

在 exit: counter-- 中,我将检查是否 (count==1) type=-1。

更好的??

i think about what you say and indeed i can see the problem - 10X!

what about adding a counter?
when the first process of the same type enter if the counter==0 type==process.type
(initialize the type)

in exit: counter-- and i will cheack if (count==1) type=-1.

better??

傲娇萝莉攻 2024-08-05 10:28:02

我想说不,目前的方法不行。

具体来说,假设类型 0 进来,并通过 Enter 进入。 然后类型 1 在类型 0 退出之前通过。 它命中互斥体,发现进程类型错误并锁定 m1。 然后类型 0 进入,解锁 m,重置进程类型,并且 m1 上的锁定仍然有效。

此外,如果有 2 个类型为 0 Enter 的进程,第一个退出的进程将导致正在运行的进程类型被重置,而进程仍处于临界区。

我假设“向上”类似于解锁,“向下”类似于锁定; 不过,这听起来像是一组用于信号量的方法。 我还假设系统只有两个互斥体(m 和 m1); 因为标记对此并不完全清楚。

编辑:您提供的建议将是这样的:

shared: this.type = -1;
mutex m, m1=1;
count=0;

enter{
    down(m)
    if (this.type == process.type) up(m1)
    down(m1)
    this.type= process.type
    count++;
    up(m)
}
exit {
    count--;
    if(count==0) this.type = -1
    up(m1)
}

这仍然存在退出在状态修改上不是线程安全的问题; 当一个新进程被唤醒/开始执行时,它的兄弟进程不会被唤醒。

我不完全确定是否有一种安全的方法可以在没有条件变量的情况下执行此操作; 除非有关于该问题的更多信息。 (即这是在“监视器”的上下文中,其中方法执行已经是线程安全的)

I'd say no, the current method is not okay.

Specifically, say a type 0 comes in, and gets through enter. type 1 then comes through before type 0 exited. It hits the mutex, finds that the process type is wrong and locks on m1. Type 0 then comes through, unlocks m, resets process type, and the lock on m1 is still in effect.

In addition, if you have 2 processes of type 0 enter, the first one to exit will cause the running process type to be reset while processes are still in the critical section.

I'm making an assumption that 'up' is akin to unlock, and 'down' is akin to lock; it sounds like a set of methods intended for a semaphore though. I'm also making an assumption that there are only two mutexes for the system (m and m1); as the markup isn't perfectly clear about that.

EDIT: The suggestion you offered would come out to something like this:

shared: this.type = -1;
mutex m, m1=1;
count=0;

enter{
    down(m)
    if (this.type == process.type) up(m1)
    down(m1)
    this.type= process.type
    count++;
    up(m)
}
exit {
    count--;
    if(count==0) this.type = -1
    up(m1)
}

This still has the problem that exit isn't thread safe on state modifications; and that when a new Process is woken/starts executing, its brethren are not woken.

I'm not entirely certain that there is a safe way to do this without Condition variables; unless there is more information about the problem available. (ie is this in the context of a 'Monitor', where method executions are already threadsafe)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文