我有这个代码,我必须让程序块反复等待信号.我的老师希望我们使用sigsuspend和面具而不是暂停或睡眠.我不熟悉的sigsuspend或面罩,我知道,sigsuspend()临时替换用面膜给出的面具调用进程的信号屏蔽,然后挂起过程,直到传递的信号,其作用是调用信号处理程序或终止进程.但是我该如何实现呢.
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
unsigned Conta = 0;
void mypause(int sign)
{
switch (sign)
{
case SIGINT:
printf("You've pressed ctrl-C\n");
printf("I'm running waiting for a signal...\n");
Conta++;
break;
case SIGQUIT:
printf("You've pressd ctrl-\\n");
printf("Number of times you've pressed CTRL-C: %d", Conta);
exit(0);
break;
}
}
int main()
{
alarm(3);
printf("I'm Alive\n");
signal(SIGINT, mypause);
signal(SIGQUIT, mypause);
printf("I'm running, waiting for a signal...\n");
while (1)
{
}
return (0);
}
Run Code Online (Sandbox Code Playgroud) 我想将来自Android Wear设备的消息发送到连接的手机.Android智能手机应用程序是用本机Java编写的.Wear-Application是用C#用Xamarin编写的,不能正常工作.这是我到目前为止所得到的:
public class MainActivity : Activity, IMessageApiMessageListener,
IGoogleApiClientConnectionCallbacks, IResultCallback
{
private IGoogleApiClient mApiClient;
private SensorManager _SensorManager;
private Sensor _Gyroscop;
private static String WEAR_MESSAGE_PATH = "//message";
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var v = FindViewById<WatchViewStub> (Resource.Id.watch_view_stub);
v.LayoutInflated += delegate {
};
if(_SensorManager == null) {
_SensorManager = (SensorManager)GetSystemService(SensorService);
}
SensorListener s = new SensorListener();
_Gyroscop = _SensorManager.GetDefaultSensor (SensorType.Gyroscope);
_SensorManager.RegisterListener (s, _Gyroscop,SensorDelay.Normal);
s.SensorChangedEvent += delegate(SensorEvent e) …Run Code Online (Sandbox Code Playgroud) 我有这个方法:
public String scanInput()
{
String input = "";
Scanner skanner = new Scanner(System.in);
while(skanner.hasNextLine()){
input = skanner.nextLine();
}
skanner.close();
return input;
}
Run Code Online (Sandbox Code Playgroud)
第一次运行此方法时,程序停止,并且在进行操作之前,系统会提示我在控制台中输入一个输入.但是,第二次运行该方法时,它会在没有暂停的情况下闪烁,并且我的程序陷入无限循环.
此方法中的哪一行使程序暂停,为什么它只在第一次暂停?
这可能是一个愚蠢的问题,但令我感到沮丧.我在循环期间在循环外添加到我的数组列表中的对象,所有数组列表都被数组中的最后一个元素覆盖.它就是这样的.
while(fin.hasNextLine())
{
String line = fin.nextLine();
String[] user = line.split(",");
r.add(new User(user[0], user[1]));
System.out.println(r.get(count).getName());
count++;
}
Run Code Online (Sandbox Code Playgroud)
在循环期间,这给了我类似的输出(USER1,USER2,USER3等).但是,在循环之后我现在有一个这样的输出(USER500,USER500,USER500).
while(fin.hasNextLine())
{
String line = fin.nextLine();
String[] user = line.split(",");
r.add(new User(user[0], user[1]));
System.out.println(r.get(count).getName());
count++;
}
for (int i =0; i < r.size(); i++)
{
System.out.println(r.get(i).getName());
}
Run Code Online (Sandbox Code Playgroud)
我设法验证这是我遇到问题的类,只有一个方法使用我注释掉的这个类中的数组列表.